Naveen PK - Thursday, February 22, 2018 2:33 PM
First, the ORDER BY in an OVER clause is different from the ORDER BY clause in a SELECT statement. Here are some quotes that show the differences:
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.
order_by_expression
Specifies a column or expression on which to sort. order_by_expression can only refer to columns made available by the FROM clause. An integer cannot be specified to represent a column name or alias.
Note the bolded section of the second quote.
Second, you're confusing the argument with the value of the argument. The argument here is the CASE expression, not the nonnegative integers 1 or 2. The only purpose of the integers is to hard-code a specific value for the CASE expression which is used for the sort. It doesn't matter what values are used as long as they define a specific sort. I could use bits, characters, dates, times, binary. I usually use integers, they're the most commonly associated with a particular order
Here are some alternate versions:SELECT *
, ROW_NUMBER() OVER(PARTITION BY ProcessID ORDER BY CASE WHEN SubType = 'TopCon' THEN CAST(0 AS BIT) ELSE 1 END, FormID) AS rn_bit
, ROW_NUMBER() OVER(PARTITION BY ProcessID ORDER BY CASE WHEN SubType = 'TopCon' THEN 'A' ELSE 'B' END, FormID) AS rn_char
, ROW_NUMBER() OVER(PARTITION BY ProcessID ORDER BY CASE WHEN SubType = 'TopCon' THEN '1900-01-01' ELSE '2000-01-01' END, FormID) AS rn_dt
, ROW_NUMBER() OVER(PARTITION BY ProcessID ORDER BY CASE WHEN SubType = 'TopCon' THEN 0xCC ELSE 0xFF END, FormID) AS rn_binary
FROM @TABLE
Also, remember that a CASE expression MUST return values of compatible types, so the output of CASE expression CANNOT refer to different columns, because there is no way to guarantee that the columns will be of compatible types.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA