Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 697
  • Last Modified:

select multiple range of records in a table

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
lirmit
Asked:
lirmit
  • 4
  • 2
1 Solution
 
imrancsCommented:

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
 
imrancsCommented:
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
 
ShogunWadeCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
imrancsCommented:
angelIII, yes i agree with you my first post has several bugs, but I have corrected them in my next post.


Imran
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Sorry, hadn't seen the second post before posting mine... good job!
0
 
imrancsCommented:
angelIII,

its okey

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now