SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help getting a count by consecutive groups


Help getting a count by consecutive groups

Author
Message
aadkins 91421
aadkins 91421
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 12
I am hoping someone can help me figure out a solution to a TSQL problem I have. I have data that in a simplified form looks like this.
Status Create Next Create
Busy 2013-09-05 14:42:46.827 2013-09-09 08:40:18.487
Busy 2013-09-09 08:40:18.487 2013-09-09 11:33:27.927
At Lunch 2013-09-09 11:33:27.927 2013-09-09 13:02:27.133
Busy 2013-09-09 13:02:27.133 2013-09-10 14:37:58.107
Busy 2013-09-10 14:37:58.107 2013-09-13 08:41:04.030

What I need to find out is how many of each status do I have consecutively.
Business rules: If a status exists then changes to another status then it is counted as a separate group. So for the example above it would be three groups: busy(2), at lunch(1), busy(2).
Any ideas? I have looked at some examples of gaps and islands but I am not sure how to use it to get the count I need here.
Keith Tate
Keith Tate
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1028 Visits: 979
This query should get you what you need:
create table CreateStatus (CreateStatus varchar(256), LastCreate datetime, NextCreate datetime);
go

insert CreateStatus
values ('Busy','2013-09-05 14:42:46.827','2013-09-09 08:40:18.487')
,('Busy','2013-09-09 08:40:18.487','2013-09-09 11:33:27.927')
,('At Lunch','2013-09-09 11:33:27.927','2013-09-09 13:02:27.133')
,('Busy','2013-09-09 13:02:27.133','2013-09-10 14:37:58.107')
,('Busy','2013-09-10 14:37:58.107','2013-09-13 08:41:04.030')
,('At Lunch','2013-09-11 11:33:27.927','2013-09-14 13:02:27.133')
;

with CTE as
(
select
CreateStatus
, LastCreate
, NextCreate
, ROW_NUMBER() over (order by CreateStatus, LastCreate) - ROW_NUMBER() over (order by LastCreate) grp
from CreateStatus
)
SELECT
CreateStatus
, count(*) as StatusCount
FROM CTE
group by
grp
, CreateStatus





Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
aadkins 91421
aadkins 91421
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 12
Thank you for your fast reply! That is perfect, just what I needed.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search