Solved

select multiple range of records in a table

Posted on 2004-09-23
7
640 Views
Last Modified: 2012-06-27
Hello Experts!

Im having problem figuring out how to ask my problem i think its best to just give the sample data and my expected result.

here is the data:

mitch,    1
mitch,    2
mitch,    3
experts, 4
experts, 5
experts, 6
experts, 7
mitch,    8
experts, 9
experts, 10
experts, 11
mitch,    12
mitch,    13


here's the result i want to have:

mitch,    1-3
mitch,    8-8
mitch,    12-13
experts, 4-7
experts, 9-11

any help please! =)
and also where can you suggest a site for advance or complex SQL articles, tutorial or forum.

thnks guys!


 
0
Comment
Question by:lirmit
  • 4
  • 2
7 Comments
 
LVL 10

Expert Comment

by:imrancs
ID: 12131967

declare @name as varchar(100)
declare @range as int
declare @PreName as varchar(100)
declare @from int
declare @to int

drop table #result
create table #result(name varchar(100), range varchar(100))

Declare Cur_Ranges Cursor for Select * from Ranges


Open  Cur_Ranges

Fetch Next From Cur_Ranges Into @name, @range
set @PreName = @name
set @from = @range

While @@Fetch_Status = 0
Begin
      if @Name = @PreName
            set @to = @range
      else
   begin
            insert into #result values(@prename, cast(@from as varchar) + ' - ' + cast(@to as varchar))
            set @from = @range
      end
set @PreName = @name
Fetch Next From Cur_Ranges Into @name, @range

End

close cur_ranges
deallocate cur_ranges

select * from #result


Imran
0
 
LVL 10

Expert Comment

by:imrancs
ID: 12132379
Sorry my above post has some errors, here is updated version alongwith the sample data

/*create table ranges(name varchar(100), range int)


insert into ranges values('mitch',    1)
insert into ranges values('mitch',    2)
insert into ranges values('mitch',    3)
insert into ranges values('experts', 4)
insert into ranges values('experts', 5)
insert into ranges values('experts', 6)
insert into ranges values('experts', 7)
insert into ranges values('mitch',    8)
insert into ranges values('experts', 9)
insert into ranges values('experts', 10)
insert into ranges values('experts', 11)
insert into ranges values('mitch',    12)
insert into ranges values('mitch',    13)
*/


declare @name as varchar(100)
declare @range as int
declare @PreName as varchar(100)
declare @from int
declare @to int

if object_id('tempdb..#result') Is Not Null
drop table tempdb..#result

create table #result(name varchar(100), range varchar(100))

Declare Cur_Ranges Cursor for Select name, range  from Ranges order by id


Open  Cur_Ranges

Fetch Next From Cur_Ranges Into @name, @range
set @PreName = @name
set @from = @range

While @@Fetch_Status = 0
Begin
      Print(@PreName + '    ' +@Name + '    ' + Cast(@Range as varchar))
      if @Name = @PreName
            set @to = @range
      else
   begin
            insert into #result values(@prename, cast(@from as varchar) + ' - ' + cast(@to as varchar))
            set @from = @range
            set @to = @range
            set @PreName = @name
      end
 
Fetch Next From Cur_Ranges Into @name, @range

End

insert into #result values(@prename, cast(@from as varchar) + ' - ' + cast(@to as varchar))

close cur_ranges
deallocate cur_ranges

select * from #result order by 1 desc




Imran
0
 
LVL 18

Accepted Solution

by:
ShogunWade earned 150 total points
ID: 12132413
This is how i would do it ( no cursors):

select small.name,small.val,MIN(big.val) FROM
      (select name,val from mytable a where not exists(select 1 from mytable where name=a.name and val=a.val-1)) small
      INNER JOIN
      (select name,val from mytable a where not exists(select 1 from mytable where name=a.name and val=a.val+1)) big
     ON small.name=big.name and small.val<=big.val
GROUP BY small.name,small.val


How it works:

1) (select name,val from mytable a where not exists(select 1 from mytable where name=a.name and val=a.val-1))  
    this returns a set of names and values where a the value - 1 doesnt exist   ie the minimums foreach range

2) (select name,val from mytable a where not exists(select 1 from mytable where name=a.name and val=a.val+1))
   this is the same but gets the maximums

3)  these are joined together by name and value but the small.val cant be smallr than the big.val

4)  at this point your results should be like this:

mitch 1 3
mitch 1 8
mitch 1 13
mitch 8 8
mitch 8 13
mitch 13 13
..etc.

5)  grouping by name and the small.val  and taking the MIN(big.val)  eliminates the eronious ones like
mitch 1 8
mitch 1 13

6)  hey presto.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 12132480
unfortunatley, imrancs' suggestion is not 100% complete (I assume not tested :-)
there are several bugs in it, here comes a tested version.
I assume the table to be called RANGES, with the columns NAME and RANGE, please adjust as your names.


declare @name as varchar(100)
declare @range as int
declare @prerange as int
declare @PreName as varchar(100)
declare @from int
declare @to int

drop table #result
create table #result(name varchar(100), range varchar(100))

Declare Cur_Ranges Cursor for Select name, range from Ranges order by name, range

Open  Cur_Ranges

Fetch Next From Cur_Ranges Into @name, @range

While @@Fetch_Status = 0
Begin
     if @prename is null
      begin
            set @prename = @name
            set @from = @range
            set @to = @range
      end
     else
     if (@Name = @PreName) and (@range = (@to + 1))
         set @to = @range
     else
        begin
          insert into #result values(@prename, cast(@from as varchar) + ' - ' + cast(@to as varchar))
          set @from = @range
        set @to = @range
        end

  set @PreName = @name
  Fetch Next From Cur_Ranges Into @name, @range
End

if @name is not null
   insert into #result values(@prename, cast(@from as varchar) + ' - ' + cast(@to as varchar))


close cur_ranges
deallocate cur_ranges

select * from #result
select * from ranges


Cheers
0
 
LVL 10

Expert Comment

by:imrancs
ID: 12132626
angelIII, yes i agree with you my first post has several bugs, but I have corrected them in my next post.


Imran
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 12132921
Sorry, hadn't seen the second post before posting mine... good job!
0
 
LVL 10

Expert Comment

by:imrancs
ID: 12133003
angelIII,

its okey

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now