如何计算和更新SQL Server表中的小计

时间:2018-12-22 10:15:09

标签: sql sql-server

我有以下格式的SQL Server表:Type列用于指示行是小计还是行级值。

如果类型为1,则为行级成本;如果类型为2,则为小计;如果类型为3,则为总计。

Category   Subcategory      Option1   Option2   Option3   Option4    Type
----------------------------------------------------------------------------
Insurance  Insurance Cost   10        20        30        40         1
Insurance  Insurance Tax    10        20        30        40         1
Insurance  Subtotal         0         0         0         0          2
Finance    Finance Cost     10        20        30        40         1
Finance    Finance Tax      10        20        30        40         1
Finance    Subtotal         0         0         0         0          2
GrandTotal GrandTotal       0         0         0         0          3
----------------------------------------------------------------------------

我要更新的行中的小计与相应类别的行级别小计以及总计与行级别总计

Category   Subcategory      Option1   Option2   Option3   Option4    Type
----------------------------------------------------------------------------
Insurance  Insurance Cost   10        20        30        40         1
Insurance  Insurance Tax    10        20        30        40         1
Insurance  Subtotal         20        40        60        80         2
Finance    Finance Cost     10        20        30        40         1
Finance    Finance Tax      10        20        30        40         1
Finance    Subtotal         20        40        60        80         2
GrandTotal GrandTotal       40        80        120       160        3
----------------------------------------------------------------------------

如何计算和更新这些行?

等待您的答复。

谢谢。

2 个答案:

答案 0 :(得分:0)

我认为cube的{​​{1}}扩展是针对您的案例的最佳套件。考虑:

group by

Rextester Demo “这仅适用于Option1列,可扩展为其他选项”

P.S。将可计算数据存储在表中是不合逻辑的,正如Dougie指出的那样,该数据可能已经作为基本数据存在于该表中。

答案 1 :(得分:0)

您正在尝试进行更新,因此有点棘手。我认为这可以解决问题:

update t
    set option1 = (case t.subcategory 
                        when 'Subtotal' then subtotal.option1
                        when 'GrandTotal' then grandtotal.option1
                        else option1
                   end),
        option2 = (case t.subcategory 
                        when 'Subtotal' then subtotal.option2
                        when 'GrandTotal' then grandtotal.option2
                        else option2
                   end),
        option3 = (case t.subcategory 
                        when 'Subtotal' then subtotal.option3
                        when 'GrandTotal' then grandtotal.option3
                        else option3
                   end),
        option4 = (case t.subcategory 
                        when 'Subtotal' then subtotal.option4
                        when 'GrandTotal' then grandtotal.option4
                        else option4
                   end)
    from t cross apply
         (select category, sum(option1) as option1, sum(option2) as option2,
                 sum(option3) as option3)
          from t t2
          where t2.category = t.category
         ) subtotal cross join
         (select category, sum(option1) as option1, sum(option2) as option2,
                 sum(option3) as option3)
          from t t2
         ) grandtotal
    where t.subcategory in ('Subtotal', 'GrandTotal');