Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is a SQL query given below, that query performs DateDIff operation by slecting max and min date according to Card ar you can say id,
here' the query
SQL
SELECT
--COUNT(DIFF)
Count(DATEDIFF(D, MinDate, MaxDate)/30) AS DIFF

FROM (
SELECT

   MAX(TDate) AS MaxDate,
   MIN(TDate) AS MinDate
   FROM EDATA
   GROUP BY TCard
)a
Group by DATEDIFF(D, MinDate, MaxDate)/30


Now i want to apply some kind of sorting in this query before performing DateDiff operation (sorting can be based on several parameters like age, name etc. but 1 at a time according to the cases)
Now I want to know How to apply cases over this query an where?
Posted

1 solution

Hi,

Try this...


SQL
ORDER BY
CASE
   WHEN cond.1 = 0 THEN 5
   WHEN cond.1 <> 0 THEN 4
   ELSE 1 END desc,
CASE
   WHEN cond.2 = 0 THEN 5
   WHEN cond.2 <> 0 THEN 4
   ELSE 9 END desc,
col1 ASC, col2 ASC, col3 ASC


Hope this will help you.

Cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900