Author |
Topic |
sent_sara
Constraint Violating Yak Guru
377 Posts |
Posted - 2014-11-06 : 00:56:28
|
[code]Table created[code]declare @t table(Qtr VARCHAR(1000),visittype VARCHAR(1000),CMSContractID VARCHAR(1000, memid VARCHAR(1000),CarrierMemID VARCHAR(1000), HeadOfHouse VARCHAR(1000),dob datetime ,FullName VARCHAR(1000),minstartdt datetime,maxenddt datetime)insert into @tSELECT '06/01/2014 - 08/31/2014','ED','H8677','CA140812115','100000003','954646','1954-04-08','TOM','2014-07-05','2014-07-05' union allSELECT '06/01/2014 - 08/31/2014','ED','H8677','CA140812115','100000003','954646','1954-04-08','TOM','2014-07-06','2014-07-06' union allSELECT '06/01/2014 - 08/31/2014','ED','H8677','CA140812115' ,'100000003','954646','1954-04-08','TOM','2014-07-09' ,'2014-07-09'[/code][/code]Requirementneed to collapse when 1 day gap between maxenddtand next record's minstartdtdataset[code]Qtr visittype CMSContractID memid CarrierMemID HeadOfHouse dob FullName minstartdt maxenddt06/01/2014 - 08/31/2014 ED H8677 CA140812115 100000003 954646 8-Apr-54 TOM 5-Jul-14 5-Jul-1406/01/2014 - 08/31/2014 ED H8677 CA140812115 100000003 954646 8-Apr-54 TOM 6-Jul-14 6-Jul-1406/01/2014 - 08/31/2014 ED H8677 CA140812115 100000003 954646 8-Apr-54 TOM 9-Jul-14 9-Jul-14[/code]Expected output:[code]Qtr visittype CMSContractID memid CarrierMemID HeadOfHouse dob FullName minstartdt maxenddt06/01/2014 - 08/31/2014 ED H8677 CA140812115 100000003 954646 8-Apr-54 TOM 5-Jul-14 6-Jul-1406/01/2014 - 08/31/2014 ED H8677 CA140812115 100000003 954646 8-Apr-54 TOM 9-Jul-14 9-Jul-14[/code] |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2014-11-06 : 05:12:42
|
What is the logic behind to get the above expected output ?--Chandu |
|
|
sent_sara
Constraint Violating Yak Guru
377 Posts |
Posted - 2014-11-06 : 09:45:20
|
we need to collapse into 1 entry instead of twofor the first two line itemsquote: Originally posted by bandi What is the logic behind to get the above expected output ?--Chandu
|
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2014-11-07 : 12:36:42
|
If you want to rollup consecutive days, try something like:WITH GrpsAS( SELECT Qtr, visittype, CMSContractID, memid, CarrierMemID, HeadOfHouse, dob, FullName, minstartdt, maxenddt ,DATEADD(day, -ROW_NUMBER() OVER ( PARTITION BY Qtr, visittype, CMSContractID, memid, CarrierMemID, HeadOfHouse, dob, FullName ORDER BY minstartdt ), minstartdt) AS Grp FROM @t), RollUpsAS( SELECT Qtr, visittype, CMSContractID, memid, CarrierMemID, HeadOfHouse, dob, FullName, Grp ,MIN(minstartdt) AS minstartdt ,MAX(maxenddt) AS maxenddt FROM Grps GROUP BY Qtr, visittype, CMSContractID, memid, CarrierMemID, HeadOfHouse, dob, FullName, Grp)SELECT Qtr, visittype, CMSContractID, memid, CarrierMemID, HeadOfHouse, dob, FullName, minstartdt, maxenddtFROM Rollups; |
|
|
|
|
|