Solved

Counting days for the month

Posted on 2011-09-09
8
199 Views
Last Modified: 2012-05-12
Given the following rows

ID       date             active
1        8/1/2011      1
1        8/10/2011    0
1        8/20/2011    1

i want to write a query that will count the number of days id=1 is active

ie

1-10 = 10
20-31 = 12

Total: 22

Allan
0
Comment
Question by:acadenilla
8 Comments
 
LVL 7

Expert Comment

by:BusyMama
Comment Utility
SELECT COUNT(*)
FROM TABLENAME
WHERE ID = 1 AND ACTIVE = 1;
0
 

Author Comment

by:acadenilla
Comment Utility
BusyMama Thanks for the reply, but that wont work your query would only give me 2.

there are only three rows here there isnt a row for every day.

the query need to count that from 8/1 - 8/10 that is was active and then from 8/21 - 8/31 it was also active for a total of 22

Allan
0
 
LVL 41

Expert Comment

by:ralmada
Comment Utility
try something like this


;with cte as (
	select a.id, a.[date], isnull(b.nextdate, dateadd(m, dateadiff(m, 0, a.[date])+1, 0)-1) as enddate
	from (select * from yourtable where active = 1) a
	cross outer apply (select id, min([date]) as nextdate from yourtable where active = 0 and id = a.id and [date] > a.[date]) b
), cte2 as (
	select id, [date], enddate, datediff(d, [date], enddate) as ndays
	from cte
)
select id, sum(ndays)
from cte2
where id = 1

Open in new window

0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
Comment Utility
and actually to limit it to a specific month
;with cte as (
	select a.id, a.[date], isnull(b.nextdate, dateadd(m, dateadiff(m, 0, a.[date])+1, 0)-1) as enddate
	from (select * from yourtable where active = 1 where [date] between '2011-08-01' and '2011-08-31') a
	cross outer apply (select id, min([date]) as nextdate from yourtable where active = 0 and id = a.id and [date] > a.[date]) b
), cte2 as (
	select id, [date], enddate, datediff(d, [date], enddate) as ndays
	from cte
)
select id, sum(ndays)
from cte2
where id = 1

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
show some proper data...

cross a month boundary... can you have


8/20/2011   1
9/04/2011   0    

?
0
 

Author Comment

by:acadenilla
Comment Utility
ralmada thx but this only gives me 9, from 8/1-8/10

I would need to add another row

select 1, '9/1/2011' 0

to my dataset to get the correct value which is not a big deal.
; WITH data AS
(
	SELECT 1 AS id, '8/1/2011' AS [date], 1 AS active UNION ALL
	SELECT 1, '8/10/2011', 0 UNION ALL
	SELECT 1, '8/20/2011', 1
), cte as (
	select a.id, a.[date], isnull(b.nextdate, dateadd(m, datediff(m, 0, a.[date])+1, 0)-1) as enddate
	from (select * from data where active = 1) a
	cross apply (select id, min([date]) as nextdate from data where active = 0 and id = a.id and [date] > a.[date] GROUP BY id) b
), cte2 as (
	select id, [date], enddate, datediff(d, [date], enddate) as ndays
	from cte
)

select id, sum(ndays)
from cte2
where id = 1
GROUP BY id

Open in new window

0
 

Author Comment

by:acadenilla
Comment Utility
@Lowfatspread

this is really only specific to one month.
0
 
LVL 41

Expert Comment

by:ralmada
Comment Utility
Here's the corrected version to give you the extra day
declare @t table (
ID       int,
[date] date,             
active int
)

insert @t values(1,        '8/1/2011',      1),
(1,        '8/10/2011',    0),
(1,        '8/20/2011',    1)

select * from @t

;with cte as (
	select a.id, a.[date], isnull(b.nextdate, dateadd(m, datediff(m, 0, a.[date])+1, 0)-1) as enddate
	from (select * from @t where active = 1 and [date] between '2011-08-01' and '2011-08-31') a
	outer apply (select id, min([date]) as nextdate from @t where active = 0 and id = a.id and [date] > a.[date] group by id) b
), cte2 as (
	select id, [date], enddate, datediff(d, [date], enddate)+1 as ndays
	from cte
)
select id, sum(ndays)
from cte2
where id = 1
group by id

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now