如何对 CONCAT GROUP BY 求和
How to SUM CONCAT GROUP BY
我有两个table第一个用户table是用户的数据第二个table是user_volumetable是用户成本量我要一个sql query and get user and get monthly sum of cost 什么是SQL查询?
user_volume
id | user_id | volume | created_at
1 | 1 | 66.00 | 2018-03-03 15:36:45
2 | 1 | 77.00 | 2018-03-03 15:36:21
3 | 1 | 88.00 | 2018-03-03 15:36:11
4 | 2 | 99.00 | 2018-03-03 19:36:15
5 | 2 | 65.05 | 2018-04-04 21:30:07
6 | 2 | 99.00 | 2018-04-04 19:36:15
7 | 2 | 65.05 | 2018-04-04 21:30:07
8 | 1 | 22.00 | 2018-04-04 15:36:45
9 | 1 | 44.00 | 2018-04-04 15:36:21
10 | 1 | 33.00 | 2018-04-04 15:36:11
11 | 2 | 13.00 | 2018-04-04 15:36:45
12 | 2 | 224.00 | 2018-04-04 15:36:21
13 | 2 | 651.00 | 2018-04-04 15:36:11
user
id | name | surname
1 | X | Y
result
user_id | date1(03-2018) | date2(04-2018)
1 | (231) | (99)
2 | (99) | (888)
您似乎需要条件聚合:
select uv.id as user_id,
sum(case when created_at >= '2018-03-01' and created_at < '2018-04-1'
then volume else 0
end) as volume_month1,
sum(case when created_at >= '2018-04-01' and created_at < '2018-05-1'
then volume else 0
end) as volume_month2
from user_volume uv
group by uv.id
我有两个table第一个用户table是用户的数据第二个table是user_volumetable是用户成本量我要一个sql query and get user and get monthly sum of cost 什么是SQL查询?
user_volume
id | user_id | volume | created_at
1 | 1 | 66.00 | 2018-03-03 15:36:45
2 | 1 | 77.00 | 2018-03-03 15:36:21
3 | 1 | 88.00 | 2018-03-03 15:36:11
4 | 2 | 99.00 | 2018-03-03 19:36:15
5 | 2 | 65.05 | 2018-04-04 21:30:07
6 | 2 | 99.00 | 2018-04-04 19:36:15
7 | 2 | 65.05 | 2018-04-04 21:30:07
8 | 1 | 22.00 | 2018-04-04 15:36:45
9 | 1 | 44.00 | 2018-04-04 15:36:21
10 | 1 | 33.00 | 2018-04-04 15:36:11
11 | 2 | 13.00 | 2018-04-04 15:36:45
12 | 2 | 224.00 | 2018-04-04 15:36:21
13 | 2 | 651.00 | 2018-04-04 15:36:11
user
id | name | surname
1 | X | Y
result
user_id | date1(03-2018) | date2(04-2018)
1 | (231) | (99)
2 | (99) | (888)
您似乎需要条件聚合:
select uv.id as user_id,
sum(case when created_at >= '2018-03-01' and created_at < '2018-04-1'
then volume else 0
end) as volume_month1,
sum(case when created_at >= '2018-04-01' and created_at < '2018-05-1'
then volume else 0
end) as volume_month2
from user_volume uv
group by uv.id