从 sql table 按日期顺序对重复值求和
Sum the duplicate values order by date from sql table
我有一个 MySQL table,其行包含 Ref_Nr 列的重复值。所以我想将点的值与 Ref_Nr、u_id 和 r_date 列相加。
id u_id r_date Points Ref_Nr
1 1 2018-04-11 1 3
2 1 2018-04-11 2 3
3 2 2018-04-11 3 4
4 2 2018-04-11 4 4
5 3 2018-04-11 6 2
6 3 2018-04-11 6 2
7 1 2018-04-10 3 3
8 1 2018-04-10 5 3
9 1 2018-04-10 2 4
10 1 2018-04-10 2 4
11 2 2018-04-10 3 3
12 2 2018-04-10 5 3
13 3 2018-04-10 2 4
14 3 2018-04-10 2 4
这是我的 sql 查询,我试过,但没有得到正确的输出
SELECT u_id, Ref_Nr ,r_date, SUM(Points) AS Points
FROM my_table ORDER BY r_date, Ref_nr,u_id;
这是预期的输出,请帮我解决这个问题
u_id r_date Points Ref_Nr
1 2018-04-11 3 3
2 2018-04-11 7 4
3 2018-04-11 12 2
1 2018-04-10 8 3
1 2018-04-10 4 4
2 2018-04-10 8 3
3 2018-04-10 4 4
您需要对数据进行分组。您不能将普通列选择与 sum()
等聚合函数混合使用
SELECT r_date, Ref_nr, u_id,
SUM(Points) AS PointSum
FROM my_table
GROUP BY r_date, Ref_nr, u_id
ORDER BY r_date, Ref_nr, u_id;
我有一个 MySQL table,其行包含 Ref_Nr 列的重复值。所以我想将点的值与 Ref_Nr、u_id 和 r_date 列相加。
id u_id r_date Points Ref_Nr
1 1 2018-04-11 1 3
2 1 2018-04-11 2 3
3 2 2018-04-11 3 4
4 2 2018-04-11 4 4
5 3 2018-04-11 6 2
6 3 2018-04-11 6 2
7 1 2018-04-10 3 3
8 1 2018-04-10 5 3
9 1 2018-04-10 2 4
10 1 2018-04-10 2 4
11 2 2018-04-10 3 3
12 2 2018-04-10 5 3
13 3 2018-04-10 2 4
14 3 2018-04-10 2 4
这是我的 sql 查询,我试过,但没有得到正确的输出
SELECT u_id, Ref_Nr ,r_date, SUM(Points) AS Points
FROM my_table ORDER BY r_date, Ref_nr,u_id;
这是预期的输出,请帮我解决这个问题
u_id r_date Points Ref_Nr
1 2018-04-11 3 3
2 2018-04-11 7 4
3 2018-04-11 12 2
1 2018-04-10 8 3
1 2018-04-10 4 4
2 2018-04-10 8 3
3 2018-04-10 4 4
您需要对数据进行分组。您不能将普通列选择与 sum()
SELECT r_date, Ref_nr, u_id,
SUM(Points) AS PointSum
FROM my_table
GROUP BY r_date, Ref_nr, u_id
ORDER BY r_date, Ref_nr, u_id;