[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

select multiple range of records in a table

Posted on 2004-09-23
7
Medium Priority
?
694 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 600 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 143

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 143

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

656 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