Solved

MSSQL get max number in range for multiple ranges

Posted on 2011-02-17
5
679 Views
Last Modified: 2012-05-11
I have a list of non-continuous numbers in a column on table 1, on table 2 of categories that show number sequences.

example
table 2
1 returns 1xxxx
2 lost  2xxxx
3 found 25xxx

table 1
14008
15001
25003

I want to find out the next number for each group.

Long way:
select * from table 2 as a
left join (select '1' as id, max(number)+ 1 as next_number from table 2 where number > 10000 and number < 20000) as b on a.id = b.id
left join (select '2' as id, max(number)+ 1 as next_number from table 2 where number > 20000 and number < 25000) as c on a.id = c.id

What is a better way of doing this?
0
Comment
Question by:freshgrill
  • 4
5 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 34921014

select id, max(number) + 1
from table2
group by id
0
 

Author Comment

by:freshgrill
ID: 34921051
@ewangoya: Table 1 just shows the number sequence, ie. any number starting with 1..... is returns, basically 10000-20000.
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 34921233

Use
with CTE (ID, Numbers)
as
(
    select 
       case 
          when number > 10000 and number < 20000 then
             1
          when number > 20000 and number < 25000 then
              2
          else
              3
        end as ID,
        number as Numbers
)
select ID, Max(Numbers) +1
from CTE
group by ID

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
ID: 34921243
Oops I missed the table
with CTE (ID, Numbers)
as
(
    select 
       case 
          when number > 10000 and number < 20000 then
             1
          when number > 20000 and number < 25000 then
              2
          else
              3
        end as ID,
        number as Numbers
      from Table2  
)
select ID, Max(Numbers) +1
from CTE
group by ID

Open in new window

0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 34921252
Sorry, its from Table1

with CTE (ID, Numbers)
as
(
    select
       case
          when number > 10000 and number < 20000 then
             1
          when number > 20000 and number < 25000 then
              2
          else
              3
        end as ID,
        number as Numbers
      from Table1  
)
select ID, Max(Numbers) +1
from CTE
group by ID
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

16 Experts available now in Live!

Get 1:1 Help Now