Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to order 2 SQL Fields in asc and desc dynamically

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?

like image 240
user532104 Avatar asked Feb 14 '11 14:02

user532104


People also ask

How do I sort by both ASC and DESC in SQL?

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.

How do you sort by 2 attributes?

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.

Can we use ORDER BY 2 desc in SQL?

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.

Can we use ORDER BY for 2 columns?

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.


1 Answers

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.

like image 115
MatBailie Avatar answered Oct 01 '22 05:10

MatBailie