SQLite3 SUM 按日期
SQLite3 SUM by date
我将 SQLite3 与 Python 一起使用,我有一些数据与此大致相似:
Date
Parent ID
Child ID
Points
Day1
1
A
5
Day1
1
B
10
Day1
2
C
20
Day2
1
A
10
Day2
1
B
15
Day2
2
C
30
我正在尝试创建一个可以显示以下内容的视图:
Date
Parent ID
Points
Day1
1
15
Day1
2
20
Day2
1
25
Day2
2
30
我试过用 GROUP BY 求和,但目前只能产生这个:
Date
Parent ID
Points
Day1
1
40
Day1
2
50
Day2
1
40
Day2
2
50
求和函数似乎在对父 class 中使用的所有数据求和,而不考虑与日期相关的分组依据语句。我如何确保点数是按天计算的,而不是全部加起来?
group by Date, ParentID
似乎工作正常:
select Date, ParentID, sum(Points) as Points
from table_name
group by Date, ParentID;
创建视图:
create view sumPoints as
select Date, ParentID, sum(Points) as Points
from table_name
group by Date, ParentID;
我将 SQLite3 与 Python 一起使用,我有一些数据与此大致相似:
Date | Parent ID | Child ID | Points |
---|---|---|---|
Day1 | 1 | A | 5 |
Day1 | 1 | B | 10 |
Day1 | 2 | C | 20 |
Day2 | 1 | A | 10 |
Day2 | 1 | B | 15 |
Day2 | 2 | C | 30 |
我正在尝试创建一个可以显示以下内容的视图:
Date | Parent ID | Points |
---|---|---|
Day1 | 1 | 15 |
Day1 | 2 | 20 |
Day2 | 1 | 25 |
Day2 | 2 | 30 |
我试过用 GROUP BY 求和,但目前只能产生这个:
Date | Parent ID | Points |
---|---|---|
Day1 | 1 | 40 |
Day1 | 2 | 50 |
Day2 | 1 | 40 |
Day2 | 2 | 50 |
求和函数似乎在对父 class 中使用的所有数据求和,而不考虑与日期相关的分组依据语句。我如何确保点数是按天计算的,而不是全部加起来?
group by Date, ParentID
似乎工作正常:
select Date, ParentID, sum(Points) as Points
from table_name
group by Date, ParentID;
创建视图:
create view sumPoints as
select Date, ParentID, sum(Points) as Points
from table_name
group by Date, ParentID;