1
set statistics time on;
DECLARE @sort_order varchar(10)
DECLARE @order_by varchar(10)
set @sort_order = 'DESC'; 
set @order_by = 'lastmodified';
WITH results AS
(
    SELECT id, title, LastModified, ROW_NUMBER() over
    (ORDER BY 
        CASE @sort_order
          WHEN 'ASC' THEN
               CASE @order_by
                    WHEN 'lastmodified' THEN CAST(LastModified AS VARCHAR(50))
                    WHEN 'title' THEN title
                    ELSE CAST(LastModified AS VARCHAR(50))
               END
          ELSE '1'
     END ASC,
     CASE @sort_order
          WHEN 'DESC' THEN
               CASE @order_by
                    WHEN 'lastmodified' THEN CAST(LastModified AS VARCHAR(50))
                    WHEN 'title' THEN title
                    ELSE CAST(LastModified AS VARCHAR(50))
               END
          ELSE '1'
     END DESC) RowNum

    FROM dbo.EmploymentOpportunities
    where CompanyId = 148

)
SELECT id, title, LastModified, (select count(*) from results) totalcount
FROM results
where RowNum between 1 and 9 OPTION(Maxdop 8)
4

1 に答える 1