Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql group on number range, not exact number

Posted on 2012-12-22
6
Medium Priority
?
454 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
[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
  • 3
  • 3
6 Comments
 
LVL 23

Accepted Solution

by:
Steve Wales earned 1332 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 71

Assisted Solution

by:Qlemo
Qlemo earned 668 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 71

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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 23

Assisted Solution

by:Steve Wales
Steve Wales earned 1332 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 71

Expert Comment

by:Qlemo
ID: 38716210
sjwales,

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

Expert Comment

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

Joys of copy and paste :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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…

705 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