简体   繁体   中英

How to sort data in stored procedure with column positions of select statement with CASE

This is not working

ORDER BY 
    CASE 
       WHEN @OrderBy = 'EndDateInDays' AND @OrderByDirection = 'D' 
          THEN 10 
    END DESC,
    CASE 
       WHEN @OrderBy = 'EndDateInDays' AND @OrderByDirection != 'D' 
          THEN 10 
    END

Where this one is working

ORDER BY 10 DESC

As per documentation,

ORDER BY order_by_expression

order_by_expression Specifies a column or expression on which to sort the query result set. A sort column can be specified as a name or column alias, or a nonnegative integer representing the position of the column in the select list.

Source: SELECT - ORDER BY Clause (Transact-SQL)

You are specifying an expression, hence the SQL Server does not sort by value in column #10. Instead it sorts your rows by a constant value '10', which results in no sort being performed.

Suggested solution

Move all complex columns into a CROSS APPLY sub-query and add another CROSS APPLY with a sorting column:

SELECT  F.Id
        ,cols.Favourite
        ,F.Agent
        ,F.Name
        ,cols.DatePublished
        ,UF.ToolTip
        ,F.CreationDate
FROM MyTable F
  INNER JOIN MyTable2 UF
    ON f.Id = UF.Id
CROSS APPLY (
  SELECT       (CASE WHEN UFF.[Id] IS NULL THEN CONVERT(BIT, 0) ELSE CONVERT(BIT, 1) END) AS Favourite
              ,CONVERT(Datetime, F.[Date] , 103) AS DatePublished
) cols
CROSS APPLY (
SELECT         -- Make sure to correctly cast all numeric and date values to text
               CASE WHEN    @OrderBy = 'EndDateInDays' THEN CONVERT(VARCHAR(100), F.[EndDateDate], 126)
                    WHEN    @OrderBy = 'Name' THEN F.Name
                    WHEN    @OrderBy = 'DatePublished' THEN cols.DatePublished
                    ELSE CONVERT(VARCHAR(100), F.Id) -- This is default sort
                  END AS [SortCol]

) sort
ORDER BY 
         CASE WHEN @OrderByDirection = 'D' THEN sort.[SortCol] END DESC
        ,CASE WHEN @OrderByDirection != 'D' THEN sort.[SortCol] END
;

You can use the column alias. Let's say it is EndDateInDays :

ORDER BY (CASE WHEN @OrderBy = 'EndDateInDays' AND @OrderByDirection = 'D' 
               THEN EndDateInDays
          END) DESC,
         (CASE WHEN @OrderBy = 'EndDateInDays' AND @OrderByDirection <> 'D' 
               THEN EndDateInDays 
          END)

If EndDateInDays is a number (as suggested by the name), you could do:

ORDER BY (CASE WHEN @OrderBy = 'EndDateInDays' AND @OrderByDirection = 'D' 
               THEN - EndDateInDays
               WHEN @OrderBy = 'EndDateInDays' AND @OrderByDirection <> 'D' 
               THEN EndDateInDays 
          END)

Also, be careful if you start combining multiple columns. It is better to have a separate CASE expression for each column to prevent inadvertent type mismatching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM