Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Group price range

Author  Topic 

usafelix
Posting Yak Master

165 Posts

Posted - 2014-11-16 : 22:45:21
I want to group by different sales amount range by each shop in below . How to write this sql query ?

select count(sales_memo),sum(salesamount) from salesheader where shopcode like '%0%' and group by a<100,b<200,c<400,d<800, z total
----------
Please help to edit the above of sql query.

shop y
A<$100 450
B<$200 50
C<$400 30
D<$800 20
Z total $550

Shop x
A<100 200
B<200 20
C<400 10
D<800 10
z total $240
--- end


gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-11-17 : 09:16:16
Try a group by clause with a case expression:


select count(sales_memo),sum(salesamount) from salesheader where shopcode like '%0%'
group by case
when a < 100 then 1
when a < 200 then 2
...
end
Go to Top of Page
   

- Advertisement -