Log in  ::  Register  ::  Not logged in

 Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Make Sequential numbers into ranges Rate Topic Display Mode Topic Options
Author
 Message
 Posted Monday, August 27, 2012 6:06 PM
 Forum Newbie Group: General Forum Members Last Login: Thursday, January 30, 2014 6:32 AM Points: 7, Visits: 36
 I have a data look like this - Note: have to be breakdown per TNID fieldTNID TN3011170 44027871003011170 44027871013011170 44027871023011170 44027871033011171 44027871043011171 44027871053011175 44027871183011175 44027871193011175 44051655553011175 44051655563011175 44051655573011175 44051655583011175 4409447976How to write a T-SQL that the output will in ranges -TNID TN1 TN23011170 4402787100 44027871033011171 4402787104 44027871053011175 4402787118 44027871193011175 4405165555 44051655583011175 4409447976 4409447976 Thanks in advance.
Post #1350652
 Posted Monday, August 27, 2012 6:52 PM
 Hall of Fame Group: General Forum Members Last Login: Today @ 6:09 AM Points: 3,516, Visits: 4,941
 This should work in SQL 2000:`CREATE TABLE #Temp (TNID INT, TN VARCHAR(20))INSERT INTO #TempSELECT 3011170, '4402787100'UNION ALL SELECT 3011170, '4402787101'UNION ALL SELECT 3011170, '4402787102'UNION ALL SELECT 3011170, '4402787103'UNION ALL SELECT 3011171, '4402787104'UNION ALL SELECT 3011171, '4402787105'UNION ALL SELECT 3011175, '4402787118'UNION ALL SELECT 3011175, '4402787119'UNION ALL SELECT 3011175, '4405165555'UNION ALL SELECT 3011175, '4405165556'UNION ALL SELECT 3011175, '4405165557'UNION ALL SELECT 3011175, '4405165558'UNION ALL SELECT 3011175, '4409447976'SELECT TNID, TNMIN=MIN(TN), TNMAX=MAX(TN)FROM #TempGROUP BY TNIDDROP TABLE #Temp` My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!
Post #1350655
 Posted Tuesday, August 28, 2012 11:15 AM
 Forum Newbie Group: General Forum Members Last Login: Thursday, January 30, 2014 6:32 AM Points: 7, Visits: 36
 I tried your query but it came out like this. The 3rd row is not correct.TNID TNMIN TNMAX3011170 4402787100 44027871033011171 4402787104 44027871053011175 4402787118 4409447976here's the missing recs:TNID TN1 TN23011175 4402787118 44027871193011175 4405165555 44051655583011175 4409447976 4409447976 Thanks for trying . Anyone out there have other ideas.
Post #1351134
 Posted Tuesday, August 28, 2012 11:20 AM
 SSCoach Group: General Forum Members Last Login: Thursday, March 06, 2014 1:52 PM Points: 15,442, Visits: 9,583
 Since the data depends on sequentiality, which isn't a property of relational data, you'll need a non-relational solution. That means a simple cursor.Step through the rows, if the TN increments by more than 1, or the TNID changes to a new value, insert the new values into a temp table as the start-range value, and update the prior row of the temp table with the last value, as the end-of-range. Then select from the temp table. - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETCProperty of The Thread"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1351140
 Posted Tuesday, August 28, 2012 11:36 AM
 SSC-Insane Group: General Forum Members Last Login: Today @ 8:13 AM Points: 22,241, Visits: 29,570
 Here is my shot at the solution.`CREATE TABLE #Temp (TNID INT, TN BIGINT);INSERT INTO #TempSELECT 3011170, 4402787100UNION ALL SELECT 3011170, 4402787101UNION ALL SELECT 3011170, 4402787102UNION ALL SELECT 3011170, 4402787103UNION ALL SELECT 3011171, 4402787104UNION ALL SELECT 3011171, 4402787105UNION ALL SELECT 3011175, 4402787118UNION ALL SELECT 3011175, 4402787119UNION ALL SELECT 3011175, 4405165555UNION ALL SELECT 3011175, 4405165556UNION ALL SELECT 3011175, 4405165557UNION ALL SELECT 3011175, 4405165558UNION ALL SELECT 3011175, 4409447976;with rCTE as (select row_number() over (order by (select null)) rn, t1.TNID, t1.TNfrom #Temp t1 left outer join #Temp t2 on t1.TNID = t2.TNID and t1.TN = t2.TN + 1where t2.TNID is nullunion allselect rn, t1.TNID, t1.TNfrom #Temp t1 inner join rCTE t2 on t1.TNID = t2.TNID and t1.TN = t2.TN + 1)select TNID, min(TN) TN1, max(TN) TN2 from rCTE group by rn, TNID order by rn,TNID;godrop table #Temp;go`
Post #1351149
 Posted Tuesday, August 28, 2012 11:54 AM
 SSCoach Group: General Forum Members Last Login: Thursday, March 06, 2014 1:52 PM Points: 15,442, Visits: 9,583
 Lynn Pettis (8/28/2012)Here is my shot at the solution.`CREATE TABLE #Temp (TNID INT, TN BIGINT);INSERT INTO #TempSELECT 3011170, 4402787100UNION ALL SELECT 3011170, 4402787101UNION ALL SELECT 3011170, 4402787102UNION ALL SELECT 3011170, 4402787103UNION ALL SELECT 3011171, 4402787104UNION ALL SELECT 3011171, 4402787105UNION ALL SELECT 3011175, 4402787118UNION ALL SELECT 3011175, 4402787119UNION ALL SELECT 3011175, 4405165555UNION ALL SELECT 3011175, 4405165556UNION ALL SELECT 3011175, 4405165557UNION ALL SELECT 3011175, 4405165558UNION ALL SELECT 3011175, 4409447976;with rCTE as (select row_number() over (order by (select null)) rn, t1.TNID, t1.TNfrom #Temp t1 left outer join #Temp t2 on t1.TNID = t2.TNID and t1.TN = t2.TN + 1where t2.TNID is nullunion allselect rn, t1.TNID, t1.TNfrom #Temp t1 inner join rCTE t2 on t1.TNID = t2.TNID and t1.TN = t2.TN + 1)select TNID, min(TN) TN1, max(TN) TN2 from rCTE group by rn, TNID order by rn,TNID;godrop table #Temp;go`You'll run into maxrecursion issues if the actual data has any significant size to it at all, with this solution. - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETCProperty of The Thread"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #1351157
 Posted Tuesday, August 28, 2012 12:06 PM
 SSC-Insane Group: General Forum Members Last Login: Today @ 8:13 AM Points: 22,241, Visits: 29,570
 GSquared (8/28/2012)Lynn Pettis (8/28/2012)Here is my shot at the solution.`CREATE TABLE #Temp (TNID INT, TN BIGINT);INSERT INTO #TempSELECT 3011170, 4402787100UNION ALL SELECT 3011170, 4402787101UNION ALL SELECT 3011170, 4402787102UNION ALL SELECT 3011170, 4402787103UNION ALL SELECT 3011171, 4402787104UNION ALL SELECT 3011171, 4402787105UNION ALL SELECT 3011175, 4402787118UNION ALL SELECT 3011175, 4402787119UNION ALL SELECT 3011175, 4405165555UNION ALL SELECT 3011175, 4405165556UNION ALL SELECT 3011175, 4405165557UNION ALL SELECT 3011175, 4405165558UNION ALL SELECT 3011175, 4409447976;with rCTE as (select row_number() over (order by (select null)) rn, t1.TNID, t1.TNfrom #Temp t1 left outer join #Temp t2 on t1.TNID = t2.TNID and t1.TN = t2.TN + 1where t2.TNID is nullunion allselect rn, t1.TNID, t1.TNfrom #Temp t1 inner join rCTE t2 on t1.TNID = t2.TNID and t1.TN = t2.TN + 1)select TNID, min(TN) TN1, max(TN) TN2 from rCTE group by rn, TNID order by rn,TNID;godrop table #Temp;go`You'll run into maxrecursion issues if the actual data has any significant size to it at all, with this solution.True, and I didn't ad the option to cap it either, but it was my first shot. I am looking at some other alternatives as well.
Post #1351165
 Posted Tuesday, August 28, 2012 12:17 PM
 SSC-Insane Group: General Forum Members Last Login: Today @ 8:13 AM Points: 22,241, Visits: 29,570
 Here is another option that appears to work for the given test data:`CREATE TABLE #Temp (TNID INT, TN BIGINT);INSERT INTO #TempSELECT 3011170, 4402787100UNION ALL SELECT 3011170, 4402787101UNION ALL SELECT 3011170, 4402787102UNION ALL SELECT 3011170, 4402787103UNION ALL SELECT 3011171, 4402787104UNION ALL SELECT 3011171, 4402787105UNION ALL SELECT 3011175, 4402787118UNION ALL SELECT 3011175, 4402787119UNION ALL SELECT 3011175, 4405165555UNION ALL SELECT 3011175, 4405165556UNION ALL SELECT 3011175, 4405165557UNION ALL SELECT 3011175, 4405165558UNION ALL SELECT 3011175, 4409447976;with basedata as (select TNID, TN, TN - row_number() over (order by TNID, TN) as grpfrom #Temp)select TNID, min(TN) as TN1, max(TN) as TN2from basedatagroup by grp, TNID;godrop table #Temp;go`
Post #1351172
 Posted Tuesday, August 28, 2012 12:24 PM
 SSCrazy Group: General Forum Members Last Login: Today @ 7:41 AM Points: 2,523, Visits: 5,357
 Lynn, your solutions are great . But this is the 7,2000 forum I wonder if that helps the OP. Luis C.I am a great believer in luck, and I find the harder I work the more I have of it. Stephen LeacockForum Etiquette: How to post data/code on a forum to get the best help
Post #1351178
 Posted Tuesday, August 28, 2012 12:25 PM
 SSC-Insane Group: General Forum Members Last Login: Today @ 8:13 AM Points: 22,241, Visits: 29,570
 Now someone tells me! Time to hit the desk with my head.
Post #1351179

 Permissions

 Copyright © 2002-2014 Simple Talk Publishing. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.