计算sum数据并在SQL中插入另一个数据

时间:2011-05-17 15:21:56

标签: sql sql-server tsql

我需要根据句号列计算小时数并插入另一个表格。请参阅下面的示例数据。 [代码]

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就是这样。

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,@ attribute

  2. 查找最大值(期间)并返回52周并计算Store_id =的总和(小时)? (在示例16和18或每个store_id中)和client_id = @client_id和attribute1 = @ attribute1和attributes = @attributes。即。总和(小时)将根据store_id更改,并且当store_id将更改时间段将更改。 查看侧面移动数据

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

    1. 将所有输出tbl_history插入另一个表中。
    2. 请给我解决方案。如果您有任何问题,请问我。

      提前致谢

1 个答案:

答案 0 :(得分:1)

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

尝试并告诉我它是否有效。

问候。