Thunder
Thunder

Reputation: 10986

Select with grouping in range

From Table
A   B
_______________
1   A
3   B
6   C
7   C
8   X   
9   Y
15  Z
16  R
17  t   
23  T
43  e

How to bring this result:

Range A  Count B
_________________
1-10    6
11-20   3
21-30   1
31-40   1

This could be done using loop:

Select Count (* ) from TableA where b between @a and @b

But I would like to do it without using any loop is it possible?

Upvotes: 5

Views: 176

Answers (1)

Tim Rogers
Tim Rogers

Reputation: 21713

Try this:

SELECT 
Cast(((A / 10) * 10 + 1) as varchar(100)) + '-' + Cast(((A / 10) * 10 + 10) as varchar(100)) AS 'Range A',
Count(*) as 'Count B'
 FROM TableA
GROUP BY A / 10

Upvotes: 2

Related Questions