user3857903
user3857903

Reputation: 52

Query to view data on Basis of Range values

I have A Table in SQL name sale count

Nodeid OfficeName Sales
1 Delhi 20
2 Mumbai 3
3 Lucknow 45
4 Kolkata 0
5 Dehradun 10
6 Pune 30

Need Help in writing such Query which will Produce result as below mentioned

Sale Range Office Count
0 to 10 3
11 to 20 1
21 to 50 2

Thanks in Advance

Upvotes: 0

Views: 51

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

You can use a case expression. I might suggest using join for this:

select v.sales_range, count(s.sales)
from (values (0, 10, '0 to 10'),
             (11, 20, '11 to 20'),
             (21, 50, '21 to 50')
     ) v(lo, hi, sales_range) left join
     sales s
     on s.sales >= v.lo and s.sales <= v.hi
group by v.sales_range
order by min(v.lo);
             

Upvotes: 1

Related Questions