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


Make Sequential numbers into ranges


Make Sequential numbers into ranges

Author
Message
fsr645
fsr645
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 49
I have a data look like this -
Note: have to be breakdown per TNID field

TNID TN
3011170 4402787100
3011170 4402787101
3011170 4402787102
3011170 4402787103
3011171 4402787104
3011171 4402787105
3011175 4402787118
3011175 4402787119
3011175 4405165555
3011175 4405165556
3011175 4405165557
3011175 4405165558
3011175 4409447976

How to write a T-SQL that the output will in ranges -

TNID TN1 TN2
3011170 4402787100 4402787103
3011171 4402787104 4402787105
3011175 4402787118 4402787119
3011175 4405165555 4405165558
3011175 4409447976 4409447976

Thanks in advance.
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7201 Visits: 6431
This should work in SQL 2000:


CREATE TABLE #Temp
(TNID INT, TN VARCHAR(20))

INSERT INTO #Temp
SELECT 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 #Temp
GROUP BY TNID

DROP 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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
fsr645
fsr645
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 49
I tried your query but it came out like this. The 3rd row is not correct.
TNID TNMIN TNMAX
3011170 4402787100 4402787103
3011171 4402787104 4402787105
3011175 4402787118 4409447976

here's the missing recs:
TNID TN1 TN2
3011175 4402787118 4402787119
3011175 4405165555 4405165558
3011175 4409447976 4409447976

Thanks for trying :-). Anyone out there have other ideas.
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23267 Visits: 9730
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, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38913 Visits: 38508
Here is my shot at the solution.



CREATE TABLE #Temp
(TNID INT, TN BIGINT);

INSERT INTO #Temp
SELECT 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;

with rCTE as (
select
row_number() over (order by (select null)) rn,
t1.TNID,
t1.TN
from
#Temp t1
left outer join #Temp t2
on t1.TNID = t2.TNID and t1.TN = t2.TN + 1
where
t2.TNID is null
union all
select
rn,
t1.TNID,
t1.TN
from
#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;

go

drop table #Temp;
go




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23267 Visits: 9730
Lynn Pettis (8/28/2012)
Here is my shot at the solution.



CREATE TABLE #Temp
(TNID INT, TN BIGINT);

INSERT INTO #Temp
SELECT 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;

with rCTE as (
select
row_number() over (order by (select null)) rn,
t1.TNID,
t1.TN
from
#Temp t1
left outer join #Temp t2
on t1.TNID = t2.TNID and t1.TN = t2.TN + 1
where
t2.TNID is null
union all
select
rn,
t1.TNID,
t1.TN
from
#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;

go

drop 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, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38913 Visits: 38508
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 #Temp
SELECT 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;

with rCTE as (
select
row_number() over (order by (select null)) rn,
t1.TNID,
t1.TN
from
#Temp t1
left outer join #Temp t2
on t1.TNID = t2.TNID and t1.TN = t2.TN + 1
where
t2.TNID is null
union all
select
rn,
t1.TNID,
t1.TN
from
#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;

go

drop 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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38913 Visits: 38508
Here is another option that appears to work for the given test data:



CREATE TABLE #Temp
(TNID INT, TN BIGINT);

INSERT INTO #Temp
SELECT 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;

with basedata as (
select
TNID,
TN,
TN - row_number() over (order by TNID, TN) as grp
from
#Temp
)
select
TNID,
min(TN) as TN1,
max(TN) as TN2
from
basedata
group by
grp, TNID
;

go

drop table #Temp;
go




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16220 Visits: 19059
Lynn, your solutions are great :-D. But this is the 7,2000 forum Sad
I wonder if that helps the OP.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38913 Visits: 38508
Now someone tells me! Time to hit the desk with my head.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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