创建一个表格,该表使用查询在另一个表的列中包含值的总和

发布于 2025-01-30 19:32:15 字数 1197 浏览 2 评论 0 原文

这是我的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日之前计算的。

我该如何实现?

This is my 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

I need to create an another table which contains cumulative sum of 'Value' column of table1 before May 15 2022 (05/15/2022 00:00) with respect to 'ID'.
I tried the below but failed:

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]

The expected output is given below.

Table2:

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

In Table2, Date column is same which indicates cumulative is calculated till May 15.

How could I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤带着心事 2025-02-06 19:32:15

首先使用适当的数据类型创建新表(没有 varchar ):

CREATE TABLE table2(Date TEXT, ID TEXT, Value INTEGER);

然后在 table1 的“ date ”列中更改日期的格式 yyyy-mm-dd 日期是可比的。

最后,使用 insert (不是 UPDATE )语句:

INSERT INTO table2 (Date, ID, Value)
SELECT date(Date), ID, SUM(Value)
FROM table1 
WHERE date(Date) < '2022-05-15'
GROUP BY date(Date), ID;

或创建新表并将行插入单个语句:

CREATE TABLE table2 AS
SELECT date(Date) AS Date, ID, SUM(Value) AS Value
FROM table1 
WHERE date(Date) < '2022-05-15'
GROUP BY date(Date), ID;

First create the new table with proper data types (there is no VARCHAR data type in SQLite):

CREATE TABLE table2(Date TEXT, ID TEXT, Value INTEGER);

Then change the format of your dates in the column Date of table1 to YYYY-MM-DD so that the dates are comparable.

Finally, use an INSERT (not UPDATE) statement:

INSERT INTO table2 (Date, ID, Value)
SELECT date(Date), ID, SUM(Value)
FROM table1 
WHERE date(Date) < '2022-05-15'
GROUP BY date(Date), ID;

Or, create the new table and insert the rows in a single statement:

CREATE TABLE table2 AS
SELECT date(Date) AS Date, ID, SUM(Value) AS Value
FROM table1 
WHERE date(Date) < '2022-05-15'
GROUP BY date(Date), ID;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文