🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.databases.ms-sqlserver
Article #102

Re: SQL statement help

#102
From: BP Margolin
Date: Tue, 29 May 2001 16:03
49 lines
2668 bytes
YA,

create table YA (Number int NOT NULL primary key)
go

insert into YA values (1)
insert into YA values (2)
insert into YA values (3)
insert into YA values (5)
insert into YA values (6)
insert into YA values (8)
insert into YA values (10)
insert into YA values (11)
insert into YA values (12)
insert into YA values (13)

select Start = n1,
            Size = (n2 - n1 + 1)
from (select min(y1.Number) as n1, min(y2.Number) as n2
           from YA as y1
           join YA as y2
              on (y1.Number <= y2.Number)
           where not exists (select * from YA as y3
                                          where y3.Number = y1.Number - 1)
           and     not exists (select * from YA as y4
                                           where y4.Number = y2.Number + 1)
           group by y1.Number) as y

-------------------------------------------
BP Margolin
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.

"YA" <ya94@hotmail.com> wrote in message news:3b13499d@news.bezeqint.net...
> I have a table on SQL Server 2000 with 1 field: Number.
> The data is like 1,2,3,6,7,10,11.
> I would like to write a SQL statement that will create a RecordSet with 2
> fields: Start, Size.
> Start: 1 , Size :3
> Start: 6 , Size :2
> Start: 10 , Size :2
> As you can see from the example I need to join all the consecutive numbers
> and how many numbers joined.
> Thanks
> ya94@hotmail.com
>
>
>
Technical Details
Message-ID: <3b13c6ae_4@news3.prserv.net>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!newsfeed.us.prserv.net!prserv.net!news3.prserv.net!32.101.37.254
References: <3b13499d@news.bezeqint.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 104de1,97746d322c30c3cd
X-Google-Attributes: gid104de1,public
X-Google-Thread: ff490,97746d322c30c3cd
X-Google-Attributes: gidff490,public
X-Google-ArrivalTime: 2001-05-29 08:59:14 PST
Reply-To: "BP Margolin" <bpmargo@attglobal.net>
From: "BP Margolin" <bpmargo@attglobal.net>
Newsgroups: comp.databases.ms-sqlserver,cz.comp.microsoft.sql
References: <3b13499d@news.bezeqint.net>
Subject: Re: SQL statement help
Date: Tue, 29 May 2001 12:03:48 -0400
Lines: 51
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
NNTP-Posting-Host: 32.101.37.254
Message-ID: <3b13c6ae_4@news3.prserv.net>
X-Trace: 29 May 2001 15:56:30 GMT, 32.101.37.254
Organization: Global Network Services - Remote Access Mail & News Services
X-Complaints-To: abuse@prserv.net
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!newsfeed.us.prserv.net!prserv.net!news3.prserv.net!32.101.37.254