Reputation: 369
I have one table like this...
+---------+------------+------+-------+
| City | NamePeople | Age | Money |
+---------+------------+------+-------+
| Paris | John | 10 | 200 |
| London | Marie | 21 | 300 |
| Paris | Kevin | 50 | 250 |
| Paris | Julia | 45 | 150 |
+---------+------------+------+-------+
I need a result with ranges , something like this
+---------------+------------+-----------------------+-------------------------+
| City | Sum(Money) | CountPeopleWithAge<30 | CountPeopleWithAge>30 |
+---------------+------------+-----------------------+-------------------------+
| Paris | 600 | 1 | 2 |
| London | 300 | 1 | 0 |
+---------------+------------+-----------------------+-------------------------+
How do I do this with sql select?
Thanks.
Upvotes: 0
Views: 152
Reputation: 2629
You can use this Query,
SELECT City,
SUM(Money),
SUM(case when Age < 30 then 1 else 0 end),
SUM(case when Age > 30 then 1 else 0 end)
FROM tableA
GROUP BY City
Upvotes: 0
Reputation: 10976
Select
City,
Sum(Money),
Sum(Case When Age < 30 Then 1 Else 0 End),
Sum(Case When Age > 30 Then 1 Else 0 End)
From
table
Group By
City
Upvotes: 4