如何正确求和和分组,SQLite 中带有 where 子句的一组记录
How to properly sum and group by, a set of records with where clause in SQLite
我有 table 个如下客户:
ID PNO PID EVENT GID A PS
1 04 1 P&R_A 2 1.0 2
2 03 1 P&R(j) 2 1.0 2
3 04 1 P&R(j) 2 1.0 2
4 04 1 P&R(j) 2 1.0 2
5 04 1 P&R(j) 2 1.0 3
6 03 1 P&R_A 2 1.0 2
7 02 1 LP 2 1.0 2
8 06 1 LP 2 0.5 1
当我运行下面的查询
SELECT PNO,EVENT, SUM(A) AS Atts, SUM(PS) AS Points FROM clients
WHERE ID = 1 GROUP BY PNO
我只得到
PNO PID EVENT GID Atts Points
02 1 LP 2 1.0 2
03 1 P&R_A 2 2.0 4
04 1 P&R(j) 2 4.0 9
06 1 P&R(j) 2 0.5 1
而不是
PNO PID EVENT GID Atts Points
02 1 LP 2 1.0 2
03 1 P&R_A 2 2.0 4
04 1 P&R(j) 2 3.0 6
04 1 P&R_A 2 1.0 3
06 1 P&R(j) 2 0.5 1
您必须在 GROUP BY 中添加两列,例如:
SELECT PNO,EVENT, SUM(A) AS Atts, SUM(PS) AS Points FROM clients
WHERE ID = 1 GROUP BY PNO, EVENT
和GROUP BY you group the entries based on the uniqueness of both PNO and EVENT and then aggregate functions (SUM in your case, but also, AVG,MIN,MAX etc.)是根据'grouping'计算的。
在分组中添加事件。
SELECT PNO,EVENT, SUM(A) AS Atts, SUM(PS) AS Points FROM clients
WHERE ID = 1 GROUP BY PNO, EVENT
我有 table 个如下客户:
ID PNO PID EVENT GID A PS
1 04 1 P&R_A 2 1.0 2
2 03 1 P&R(j) 2 1.0 2
3 04 1 P&R(j) 2 1.0 2
4 04 1 P&R(j) 2 1.0 2
5 04 1 P&R(j) 2 1.0 3
6 03 1 P&R_A 2 1.0 2
7 02 1 LP 2 1.0 2
8 06 1 LP 2 0.5 1
当我运行下面的查询
SELECT PNO,EVENT, SUM(A) AS Atts, SUM(PS) AS Points FROM clients
WHERE ID = 1 GROUP BY PNO
我只得到
PNO PID EVENT GID Atts Points
02 1 LP 2 1.0 2
03 1 P&R_A 2 2.0 4
04 1 P&R(j) 2 4.0 9
06 1 P&R(j) 2 0.5 1
而不是
PNO PID EVENT GID Atts Points
02 1 LP 2 1.0 2
03 1 P&R_A 2 2.0 4
04 1 P&R(j) 2 3.0 6
04 1 P&R_A 2 1.0 3
06 1 P&R(j) 2 0.5 1
您必须在 GROUP BY 中添加两列,例如:
SELECT PNO,EVENT, SUM(A) AS Atts, SUM(PS) AS Points FROM clients
WHERE ID = 1 GROUP BY PNO, EVENT
和GROUP BY you group the entries based on the uniqueness of both PNO and EVENT and then aggregate functions (SUM in your case, but also, AVG,MIN,MAX etc.)是根据'grouping'计算的。
在分组中添加事件。
SELECT PNO,EVENT, SUM(A) AS Atts, SUM(PS) AS Points FROM clients WHERE ID = 1 GROUP BY PNO, EVENT