0

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.

Laurence
  • 10,896
  • 1
  • 25
  • 34
luckasx
  • 359
  • 1
  • 6
  • 20

2 Answers2

4
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
Laurence
  • 10,896
  • 1
  • 25
  • 34
0

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
Viji
  • 2,629
  • 1
  • 18
  • 30