计算总和数据并插入到 SQL 中的另一个数据中

发布于 2024-11-07 15:00:39 字数 2423 浏览 1 评论 0原文

我需要根据期间列计算小时总和并插入到另一个表中。请参阅下面的示例数据。 [code]

tbl_Summary

    Client_id   Store_id    attribute1  Period    Hours     attributes
1              16             1        2/25/2007  1054.8     1
1              16             1        3/11/2007  1112.8     1
1              16             1        3/25/2007  1164.8     1
1              16             1        4/8/2007   1383.2     1
1              16             1        4/22/2007  1351.6     1
1            16       1          9/21/2008      1523.6       1**
1            16       1         10/5/2008      1020.26   1
1            16       1         10/19/2008      939.94   1
1            16       1         11/2/2008       903.14   1
1            16       1         8/9/2009        866.66   1
1            16       1         8/23/2009       915.48   1
1            16       1         9/6/2009        894.26   1
1            16       1         9/20/2009      1458.58   1[/b]
1              18             1        6/1/2008   1112.8     1
1              18             1        6/15/2008  1164.8     1
1              18             1        6/29/2008  1383.2     1 [b]
1            18        1     7/13/2008 1351.6    1
1            18        1    12/28/2008 1523.6    1
1            18        1     1/11/2009  979.2    1
1            18        1     1/25/2009  913.2    1
1            18        1     2/8/2009   930.6    1
1            18        1     2/22/2009 1143.4    1
1            18        1     5/31/2009 1066.16   1
1            18        1     6/14/2009  1174.8   1
1            18        1     6/28/2009  1099.2   1
1            18        1     7/12/2009  1014.5   1 

输出表将是这样的。

tbl_history
--------------
[code]client_id store_id    attribute1  hours   attributes
1                    16          1         8521.92    1
1                    18          1        11196.92    1

sp 的条件

  1. sp 的参数只有@client_id、@attribute1、@attributes

  2. 查找 Max(Period) 并返回到 52 周并计算 Sum(Hours) 其中 Store_id= ? (在示例 16 和 18 或每个 store_id 中),client_id= @client_id,attribute1 = @attribute1,attributes = @attributes。 IE。 Sum(Hours) 将根据 store_id 发生变化,当 store_id 发生变化时,时段也会发生变化。 查看侧移数据

store_id = 16 and period=9/20/2009 至 9/20/2009 sum(hours) =8521.92

  1. 将所有输出 tbl_history 插入到另一个表中。

请给我解决方案。如果您有任何问题请问我。

提前致谢

I need to calculate sum of hours based on period column and insert into another table. Please see below example data.
[code]

tbl_Summary

    Client_id   Store_id    attribute1  Period    Hours     attributes
1              16             1        2/25/2007  1054.8     1
1              16             1        3/11/2007  1112.8     1
1              16             1        3/25/2007  1164.8     1
1              16             1        4/8/2007   1383.2     1
1              16             1        4/22/2007  1351.6     1
1            16       1          9/21/2008      1523.6       1**
1            16       1         10/5/2008      1020.26   1
1            16       1         10/19/2008      939.94   1
1            16       1         11/2/2008       903.14   1
1            16       1         8/9/2009        866.66   1
1            16       1         8/23/2009       915.48   1
1            16       1         9/6/2009        894.26   1
1            16       1         9/20/2009      1458.58   1[/b]
1              18             1        6/1/2008   1112.8     1
1              18             1        6/15/2008  1164.8     1
1              18             1        6/29/2008  1383.2     1 [b]
1            18        1     7/13/2008 1351.6    1
1            18        1    12/28/2008 1523.6    1
1            18        1     1/11/2009  979.2    1
1            18        1     1/25/2009  913.2    1
1            18        1     2/8/2009   930.6    1
1            18        1     2/22/2009 1143.4    1
1            18        1     5/31/2009 1066.16   1
1            18        1     6/14/2009  1174.8   1
1            18        1     6/28/2009  1099.2   1
1            18        1     7/12/2009  1014.5   1 

Out put table will be like this.

tbl_history
--------------
[code]client_id store_id    attribute1  hours   attributes
1                    16          1         8521.92    1
1                    18          1        11196.92    1

Conditions for sp

  1. Parameters to sp are only @client_id, @attribute1, @attributes

  2. Find Max(Period) and go back to 52 weeks and calculate Sum(Hours) where Store_id= ? (in example 16 and 18 or each store_id) and client_id= @client_id and attribute1 = @attribute1 and attributes = @attributes. ie. Sum(Hours) will change based on store_id and when store_id will change period will change. See side moved data

store_id = 16 and period=9/20/2009 to 9/20/2009 sum(hours) =8521.92

  1. Insert into another table all output tbl_history.

Please give me solution. If you have any question ask me.

Thanks in advance

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

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

发布评论

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

评论(1

柠檬 2024-11-14 15:00:39
INSERT INTO tbl_history
SELECT client_id, store_id, attribute1, SUM(Hours), attributes
FROM tbl_Summary
WHERE PERIOD <=
(
    SELECT MAX(PERIOD)
    FROM tbl_Summary
    WHERE client_id = @client_id
    AND attribute1 = @attribute1
    AND attributes = @attributes
)
AND PERIOD  >= 
(
    SELECT (MAX(PERIOD) - 1 year) --(I dont remember the sintax for sql getdate() something but thats the idea)
    FROM tbl_Summary
    WHERE client_id = @client_id
    AND attribute1 = @attribute1
    AND attributes = @attributes
)
AND client_id = @client_id
AND attribute1 = @attribute1
AND attributes = @attributes
GROUP BY client_id, store_id, attribute1, attributes

尝试一下并告诉我是否有效。

问候。

INSERT INTO tbl_history
SELECT client_id, store_id, attribute1, SUM(Hours), attributes
FROM tbl_Summary
WHERE PERIOD <=
(
    SELECT MAX(PERIOD)
    FROM tbl_Summary
    WHERE client_id = @client_id
    AND attribute1 = @attribute1
    AND attributes = @attributes
)
AND PERIOD  >= 
(
    SELECT (MAX(PERIOD) - 1 year) --(I dont remember the sintax for sql getdate() something but thats the idea)
    FROM tbl_Summary
    WHERE client_id = @client_id
    AND attribute1 = @attribute1
    AND attributes = @attributes
)
AND client_id = @client_id
AND attribute1 = @attribute1
AND attributes = @attributes
GROUP BY client_id, store_id, attribute1, attributes

Try it and tell me if it worked.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文