I want to Order a SQL Select Query where there's 2 fields that are in the order by. I then need to decide if one is Descending and the other as Ascending. How is this done
I want something like:
Select * from Customer
Order By Date @asc_or_Desc_date, Name @asc_or_Desc_name
Anyone got any ideas?
I have tried this but it seems to fail
SELECT
Customer_ID,
Name,
Age
FROM #Customer
ORDER BY
CASE WHEN @fieldSort ='Name'
THEN ROW_NUMBER() over (order by Name) *
case when @directionOfSort = 'A'
THEN 1 ELSE -1 END,
ROW_NUMBER() over (order by Age) *
case when @directionOfSort = 'A'
THEN 1 ELSE -1 END,
END
Anyone know how to sort this?
If you want to sort some of the data in ascending order and other data in descending order, then you would have to use the ASC and DESC keywords. SELECT * FROM table ORDER BY column1 ASC, column2 DESC; That is how to use the ORDER BY clause in SQL to sort data in ascending order.
After the ORDER BY keyword, add the name of the column by which you'd like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example, last_name ). You can modify the sorting order (ascending or descending) separately for each column.
If you prefer, you can use the positions of the column in the ORDER BY clause. See the following statement: SELECT name, credit_limit FROM customers ORDER BY 2 DESC, 1; In this example, the position of name column is 1 and credit_limit column is 2.
You can also ORDER BY two or more columns, which creates a nested sort . The default is still ascending, and the column that is listed first in the ORDER BY clause takes precedence. The following query and Figure 3 and the corresponding query results show nested sorts.
SELECT
Customer_ID,
Name,
Age
FROM
#Customer
ORDER BY
CASE WHEN @field = 'Name' AND @direction = 'A' THEN Name ELSE NULL END ASC,
CASE WHEN @field = 'Name' AND @direction = 'D' THEN Name ELSE NULL END DESC,
CASE WHEN @field = 'Age' AND @direction = 'A' THEN Age ELSE NULL END ASC,
CASE WHEN @field = 'Age' AND @direction = 'D' THEN Age ELSE NULL END DESC
I wouldn't want to do that over many different combinations though. If you have a lot of combinations I'd do somethign based on the following...
SELECT
Customer_ID,
Name,
Age
FROM
(
SELECT
Customer_ID,
Name,
ROW_NUMBER() OVER (ORDER BY Name) AS "name_order",
Age,
ROW_NUMBER() OVER (ORDER BY Age) AS "age_order"
FROM
#Customer
)
AS [data]
ORDER BY
CASE @field1
WHEN 'Name' THEN CASE @direction1 WHEN 'A' THEN name_order ELSE -name_order END
WHEN 'Age' THEN CASE @direction1 WHEN 'A' THEN age_order ELSE -age_order END
ELSE NULL
END,
CASE @field2
WHEN 'Name' THEN CASE @direction2 WHEN 'A' THEN name_order ELSE -name_order END
WHEN 'Age' THEN CASE @direction2 WHEN 'A' THEN age_order ELSE -age_order END
ELSE NULL
END
Repeat as many times as is required...
Note: Just because it can be done this way, doesn't mean it should be done this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With