January 11, 2011 at 1:25 pm
Hello everyone,
I have a data set that contains 5 records.
create table #myTable
(clientId int, enrollDate smalldatetime, exitdate smalldatetime, fundingsource int)
insert into #myTable values(1,'2009-03-23','2010-07-31',1)
insert into #myTable values(1,'2010-08-01','2010-08-31',2)
insert into #myTable values(1,'2010-09-01','2010-09-03',1)
insert into #myTable values(1,'2009-09-07',NULL,1)
I need to pick the earliest enrolldate for each funding source when there is no interruption in funding sources. So in the data example I would need to pick 9/1/2010 for the earliest enrolldate for funding source 1 and 8/1/2010 for funding source 2. Even though I have an enroll date of 3/23/2009 for funding source 1 the record doesn't count because there is a different funding source recorded in the middle of the set.
January 11, 2011 at 7:42 pm
Heh... it's not as easy as it looks, huh?
Thank you for setting up the sample data correctly. This should do it for you... the CTE's are named after what they do.
WITH
cteEnumerateGroups AS
(
SELECT FundingGroup = ROW_NUMBER() OVER (ORDER BY EnrollDate)
- ROW_NUMBER() OVER (PARTITION BY FundingSource ORDER BY EnrollDate),
EnrollDate, FundingSource
FROM #MyTable
),
cteEnumerateRowsByGroupDate AS
(
SELECT RowNum = ROW_NUMBER() OVER (PARTITION BY FundingSource ORDER BY FundingGroup DESC, EnrollDate ASC),
EnrollDate, FundingSource
FROM cteEnumerateGroups
)
SELECT FundingSource, EnrollDate
FROM cteEnumerateRowsByGroupDate
WHERE RowNum = 1
;
--Jeff Moden
Change is inevitable... Change for the better is not.
January 12, 2011 at 10:22 am
Thanks. It looks like it does exactly what I need. I would like to learn more about how this works. Got any good reference sources to recommend?
January 12, 2011 at 10:57 am
jwmott (1/12/2011)
Thanks. It looks like it does exactly what I need. I would like to learn more about how this works. Got any good reference sources to recommend?
I'm taking a guess here, but you're probably asking about what the common table expressions and row_number() functions are doing.
The best reference sources for these are BOL - just highlight "WITH" or "ROW_NUMBER()", and press the F1 key.
You might also want to read this article on SQL Server Ranking Functions[/url].
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
January 12, 2011 at 11:25 am
I now have learned what WITH does and ROW NUMBER. On a quest to figure out OVER and PARTITION.
I am now trying to apply the suggested solution to a dataset with more than one client. The solution provided seems to provide the earliest enrollment date in each possible funding source for a single client. I need to provide the earliest enrollment date for each funding source for all clients
drop table #myTable
create table #myTable(clientId int, enrollDate smalldatetime, exitdate smalldatetime, fundingsource int)
insert into #myTable values(1,'2009-03-23','2010-07-31',1)
insert into #myTable values(1,'2010-08-01','2010-08-31',2)
insert into #myTable values(1,'2010-09-01','2010-09-03',1)
insert into #myTable values(1,'2009-09-07',NULL,1)
insert into #myTable values(2,'2009-02-17','2010-09-03',1)
insert into #myTable values(2,'2010-09-07',NULL,1)
Thanks to everyone who provides help. I have learned a lot and I greatly appreciate the help.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply