Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table like this
id          Attendate
1	5/25/2012 12:00:00 AM
2	6/25/2012 12:00:00 AM
3	9/25/2012 12:00:00 AM
4	5/25/2012 12:00:00 AM
5	7/25/2012 12:00:00 AM
6	8/25/2012 12:00:00 AM
7	9/25/2012 12:00:00 AM
8	5/25/2012 12:00:00 AM
9	6/25/2012 12:00:00 AM
10	8/25/2012 12:00:00 AM

I want the sum of the days in the months(Calendar days) from that table

I want the output:
153


HI ,Actually I need to add The repeating months only once ;Means i need days of distinct month;
Eg:There is only 5 months,
5 may
6 June
7 July
8 August
9 September

I need the sum of these months ,

Which means the distinct month of dates provided in the table
Posted
Updated 16-Jul-12 18:00pm
v3

Try something like
SQL
SELECT DATEDIFF(
(select top 1 AttenDate from myTable orderby AttenDate Desc)
(select top 1 AttenDate from myTable orderby AttenDate Asc)
GETDATE()) AS NumberOfDays 

This should give you the difference between the maximum and minimum dates in your table.

You could optimize this query by using JOIN. You can try that out for yourself.
 
Share this answer
 
Try:
SQL
SELECT SUM(DATEDIFF(DAY, DATEADD(DAY, 1 - DAY(Attendate), Attendate),
                         DATEADD(MONTH, 1, DATEADD(DAY, 1 - DAY(Attendate), Attendate))))
FROM MyTable
 
Share this answer
 
Comments
sandeep nagabhairava 17-Jul-12 2:11am    
my 5!
SQL
select datediff(day,(select top 1 Attendate from  table order by Attendate asc) ,(select top 1 Attendate from table  order by Attendate desc)) 
 
Share this answer
 
DECLARE @totaldays TABLE (months int)
declare @Days bigint
declare @count int
set @count=5
INSERT INTO @totaldays (months)
select distinct(Month(Attendate)) from pr_empdailyattendance where deptid=2 and finyearid=11


SELECT SUM(DATEDIFF(DAY, DATEADD(DAY, 1 - DAY(months), months),
DATEADD(MONTH, 1, DATEADD(DAY, 1 - DAY(months), months))))
FROM @totaldays
 
Share this answer
 
v2

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