Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Counting days for the month

Posted on 2011-09-09
8
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 7

Expert Comment

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

Author Comment

by:acadenilla
ID: 36512857
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
ID: 36513254
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 41

Accepted Solution

by:
ralmada earned 2000 total points
ID: 36513265
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
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36513422
show some proper data...

cross a month boundary... can you have


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

?
0
 

Author Comment

by:acadenilla
ID: 36513670
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
ID: 36513679
@Lowfatspread

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

Expert Comment

by:ralmada
ID: 36514263
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

688 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