以列方式查找行的运行总和,并将其显示在同一数据集中的另一行中

时间:2017-11-02 10:03:34

标签: sql sql-server

我需要一些帮助来计算行列的运行SUM并将其显示在同一数据集的另一行中。

我有一张这样的表:

CREATE TABLE #tempTable
(ObservationDate VARCHAR(100),
IOLinetype      VARCHAR(100),
PatInfoID       VARCHAR(100),
LineTypeName    VARCHAR(100),
LineName        VARCHAR(100),
LineNameString  VARCHAR(100),
LineType        VARCHAR(100),
[00:00]         INT,
[01:00]         INT,
[02:00]         INT,
[03:00]         INT,
[04:00]         INT
);

使用这样的数据:

INSERT INTO #tempTable VALUES
('2017-10-25','INPUT','839','Hourly Input', 'Hourly Input', 'Hourly Input','5.5', 1, 2, 3, 4, 5),
('2017-10-25','OUTPUT','839','Hourly Output', 'Hourly Output', 'Hourly Output','5.5', 1, 2, 3, 4, 5),
('2017-10-26','INPUT','839','Hourly Input', 'Hourly Input', 'Hourly Input','5.5', 1, 2, 3, 4, 5),
('2017-10-26','OUTPUT','839','Hourly Output', 'Hourly Output', 'Hourly Output','5.5', 1, 2, 3, 4, 5)

enter image description here

我希望结果如此: enter image description here

在这方面的帮助将不胜感激。 感谢。

3 个答案:

答案 0 :(得分:1)

您可以使用union all

select ObservationDate, IOLinetype, PatInfoID, LineTypeName, LineName, LineNameString, LineType,
       [00:00], [01:00], [02:00], [03:00], [04:00] 
from #tempTable
union all
select ObservationDate, 'TOTAL INPUT', PatInfoID, 'Total Input', 'Total Input', 'Total Input', '5.6',
       [00:00], [00:00] + [01:00], [00:00] + [01:00] + [02:00],
       [00:00] + [01:00] + [02:00] + [03:00], 
       [00:00] + [01:00] + [02:00] + [03:00] + [04:00] ;

您的原始数据没有明显的排序。如果确实如此,您可以按此订购。例如,如果ObservationDate有时间组件:

order by ObservationDate, LineType

答案 1 :(得分:0)

您可以使用UNPIVOT将列旋转为列值,使用SUM()计算运行总计(请参阅How to Find Running Total in SQL Server博客),然后使用PIVOT将结果旋转回列值

SELECT t.*
FROM (

    -- Original table
    SELECT ObservationDate, IOLinetype, PatInfoID, LineTypeName, LineName, LineNameString, LineType
           ,[00:00], [01:00], [02:00], [03:00], [04:00]
    FROM #tempTable

    UNION ALL

    -- Pivoted Running total
    SELECT   pvt.ObservationDate
            ,'TOTAL ' + pvt.IOLinetype
            ,PatInfoID, LineTypeName, LineName, LineNameString, LineType
            ,[00:00], [01:00], [02:00], [03:00], [04:00]
    FROM (
        -- Unpivoted data with a Running total
        SELECT   ObservationDate, IOLinetype, PatInfoID, LineTypeName, LineName, LineNameString, LineType, Time
                ,SUM(unpvt.[Value]) OVER (
                        PARTITION BY unpvt.ObservationDate ,unpvt.IOLinetype
                        ORDER BY unpvt.ObservationDate
                        ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
                    AS Value_Total
        FROM #tempTable t
        UNPIVOT (
            [Value] FOR [Time] IN ([00:00], [01:00], [02:00], [03:00], [04:00])
        ) unpvt
    ) rt
    PIVOT (
        SUM(Value_Total)
        FOR [Time]  IN ([00:00], [01:00], [02:00], [03:00], [04:00])
    ) pvt
) t

-- Sort everything
ORDER BY 1, LineTypeName, CASE WHEN IOLinetype LIKE 'TOTAL%' THEN 0 ELSE 1 END DESC
;

答案 2 :(得分:0)

首先,我强烈建议更改此表的结构。关于每小时输入的数据应该保存在不同的表格中。 为了达到这个目的,我可能会尝试将表格(使用UNPIVOT运算符或交叉应用)取消,然后尝试对组中的值进行求和。