创建一个表格,该表使用查询在另一个表的列中包含值的总和
这是我的sqlite table1:
SN Date ID Value
0 05/15/2022 23:50 9761 433
1 05/20/2022 23:50 9761 440
2 05/16/2022 23:50 9761 0
3 05/13/2022 23:50 9761 0
4 05/19/2022 23:50 9761 0
5 05/15/2022 23:50 10290 376
6 05/20/2022 23:50 10290 323
7 05/14/2022 23:50 10290 324
8 05/11/2022 23:50 10290 0
9 05/16/2022 23:50 10290 0
10 05/19/2022 23:50 9988 401
11 05/15/2022 23:50 9988 224
12 05/11/2022 23:50 9988 0
13 05/10/2022 23:50 9988 175
14 05/20/2022 23:50 9988 0
我需要创建另一个表,该表包含累积的``值''列列的“值”列的累积总和。 我尝试了以下操作,但失败了:
CREATE TABLE table2( Date varchar(100), ID varchar(50), Value INT)
UPDATE [table2]
SET ([Date],[ID],[Value]) = ([table1].[Date],[table1].[ID],sum([table1].[Value])
FROM [table1]
WHERE [table1].[Date] < date('2022-05-15') and
GROUP BY [table1].[ID]
下面给出了预期输出。
表2:
SN Date ID Value
0 05/15/2022 00:00 9761 0
1 05/15/2022 00:00 10290 324
2 05/15/2022 00:00 9988 175
在表2中,日期列相同,这表明累积是在5月15日之前计算的。
我该如何实现?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先使用适当的数据类型创建新表(没有
varchar
):然后在
table1
的“date
”列中更改日期的格式yyyy-mm-dd
日期是可比的。最后,使用
insert
(不是UPDATE
)语句:或创建新表并将行插入单个语句:
First create the new table with proper data types (there is no
VARCHAR
data type in SQLite):Then change the format of your dates in the column
Date
oftable1
toYYYY-MM-DD
so that the dates are comparable.Finally, use an
INSERT
(notUPDATE
) statement:Or, create the new table and insert the rows in a single statement: