Solved

Grouping SQL Results Based on certain ranges in an int field.MS SQL 2008

Posted on 2011-02-26
5
336 Views
Last Modified: 2012-05-11
I have a query which returns the results i need, will be putting this into a view as i'll need this for a few different things, one of the things i need it for is to group based on a particular field.

the field is called length, it is in kilometres, i want to group records based on the length as follows:

0-5 = group1
5-10 = group 2
10-20 = group 3
20-40 = group 4
>40 = group 5

could i use a case statement within the view to generate a column with the group name based on the ranges above, and then do a query on this view. grouping by that group column to collate the results?

to keep it simple please assume the following table:
id, roadname, city, state, roadlength

where roadlength is the column i want to group on.


0
Comment
Question by:HRT_HSV
[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
5 Comments
 
LVL 9

Assisted Solution

by:sshah254
sshah254 earned 125 total points
ID: 34990258
Yes, you can use a case statement.

case
  when length between 0 and 5 then 'group1'
  when length between 6 and 10 then 'group2'
  ...
end case

Ss
0
 
LVL 18

Accepted Solution

by:
sventhan earned 125 total points
ID: 34991300
select roadname, city, state,
case
  when length between 0 and 5 then 'group1'
  when length between 6 and 10 then 'group2'
  when length between 11 and 20 then 'group3'
  when length between 21 and 40 then 'group4'
  when length>41 then 'group5' end
end as mygroup
from yourtable
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 34991404
Yes, both are right. If you want the view only for grouping purposes, and nothing else, you won't need that; you can use ad-hoc views (aka derived tables)
select kmgroup, min(roadlength), max(roadlength), avg(roadlength)
from
 (select *, 
    case when roadlength <=  5 then 'group1'
         when roadlength <= 10 then 'group2'
         when roadlength <= 20 then 'group3'
         when roadlength <= 40 then 'group4'
         else 'group5'
    end kmgroup
  from kmtable
 ) grptbl
group by kmgroup

Open in new window

In addition this example shows that you won't need to use the between in case.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34994096
yes you can use a case statement to do this but you would be much better off if you actually set up
a table and used that in your view to give the grouping/range name
0
 

Author Closing Comment

by:HRT_HSV
ID: 34996249
split points as first 2 comments provided the complete solution.  1st comment confirmed logic but 2nd comment had complete code.

Much appreciated everyone who provided solution thank you
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
push and Pull replication 31 48
SQL Select - Help finding duplicate records 5 25
Connect Gridview column to Textbox in C# 2 41
Need SQL Update 2 11
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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…

730 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