Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i applied this query over my data in SQL server for calculating difference between max and min date group by Some id, my query is as follows

SQL
SELECT COUNT(DIFF)
MaxDate,
MinDate,
DATEDIFF(D, MinDate, MaxDate)/30 AS DIFF
FROM (
SELECT
   MAX(TDate) AS MaxDate,
   MIN(TDate) AS MinDate
   FROM EDATA
   GROUP BY TCard
)a


This query is producing result as

SQL
DIFF
10
5
10
10
5
7


Now i want to apply count operation over this to calculate occurring of particular difference as

SQL
Count
3
2
1


I want to know how to apply count operation over my query for that particular functionality, mentioned above(for count)
Posted

1 solution

Hi,

Check this 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
 
Share this answer
 
Comments
Abhishek Jaiswall 29-Aug-14 3:21am    
Thanks Buddy! :)
syed shanu 29-Aug-14 3:31am    
i check with my table its working fine :
SELECT
--COUNT(DIFF)
Count(DATEDIFF(D, MinDate, MaxDate)/30) AS DIFF

FROM (
SELECT

MAX(IN_DATE) AS MaxDate,
MIN(IN_DATE) AS MinDate
FROM Operation
GROUP BY PROJ_ID
)a
Group by DATEDIFF(D, MinDate, MaxDate)/30

and my result like this :
Diff
104
31
38
36
Abhishek Jaiswall 29-Aug-14 4:24am    
yeah, there was some other error, now its working fine. Thanks!
Abhishek Jaiswall 29-Aug-14 4:41am    
I have one more question,
if i want to apply some more functionality(query operations) in that query for some extended functionality, then in case where am suppose to use WHERE clause (conditions can be several).
like if i want to sort data before doing date difference operation on the basic of Name, gender etc. from that
syed shanu 29-Aug-14 4:44am    
You can do that try with your query if need support comment here

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