Solved

sql group on number range, not exact number

Posted on 2012-12-22
6
444 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

813 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

12 Experts available now in Live!

Get 1:1 Help Now