Solved

sql group on number range, not exact number

Posted on 2012-12-22
6
449 Views
Last Modified: 2012-12-23
I have a column with a decimal which is a percentage .76, .24, 1.

I want to group on this column, but in .1 pt range, meaning group 0-.09, .1-.19, .2-.29.
0
Comment
Question by:jackjohnson44
  • 3
  • 3
6 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 333 total points
ID: 38716189
I used this as an example:

use demo
go
create table tab1 (student char(20), score decimal(9,2))

insert into tab1 values ('Fred', .74)
insert into tab1 values ('Mary', .72)
insert into tab1 values ('Ann', .69)
insert into tab1 values ('George', .62)
insert into tab1 values ('Mark', .76)
insert into tab1 values ('Bob', .54)

select round(score, 1, 1), count(*)
from tab1
group by round(score, 1, 1)

                                        
--------------------------------------- -----------
0.50                                    1
0.60                                    2
0.70                                    3

(3 row(s) affected)

Open in new window


Still tweaking on making it display the range you want as a literal
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 167 total points
ID: 38716192
You can group by an expression, so adding
  convert(int, percent*10)/10.0
to your group by should do. But since it is usually required to see the group itself in the output, it's better to use an in-place table:
select a,b, percent_grp, count(*)
from (
  select a, b, convert(int, percent*10)/10.0 as percent_grp
  from tbl
) x
group by a, b, percent_grp

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 38716197
Output as range text can be managed with
    convert(varchar(4), percent_grp) + ' - ' + convert(varchar(4), percent_grp + 0.09)
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 333 total points
ID: 38716203
Here, this would do it and show you the range in the output.

Inner select does the grouping (and so can use indexes since you're not putting functions in the group bys and what not which might invalidate them) and then the outer select does the formatting of that output

select convert(char(4), round(score, 1, 1))+'-'+convert(char(4), round(score, 1, 1)+0.09) as score_range, total
from
(
select round(score, 1, 1) as score, count(*) as total
from tab1
group by round(score, 1, 1)
) as x
order by score_range

score_range total
----------- -----------
0.50-0.59   1
0.60-0.69   2
0.70-0.79   3

(3 row(s) affected)

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 38716210
sjwales,

score is now truncated twice, which does not matter for the result, but is superfluous.
0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38716269
Oops!  You're right:

Joys of copy and paste :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

756 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