简体   繁体   中英

How to calculate and update subtotal in SQL Server table

I have a SQL Server table in following format: Type column is to indicate whether row is Subtotal or line level value.

If type is 1, then it is line level cost, and if type is 2, then it is subtotal and if type is 3, then it is grand total.

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
----------------------------------------------------------------------------

I want to update rows with subtotal with line level subtotal of respective category and grand total with line level total

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
----------------------------------------------------------------------------

How can I calculate and update these rows?

Awaiting for your reply.

Thanks.

I think cube extension for group by best suites for your case. Consider :

select distinct max(Category) as Category, Subcategory, 
       sum(Option1) as Option1
       sum(Option2) as Option2,
       sum(Option3) as Option3,
       sum(Option4) as Option4    
  from t
 where type = 1
 group by cube(Category,Subcategory)
 order by Category, Subcategory desc;

Rextester Demo "This is for Option1 column only, expandable for other options"

PS it's illogical to store a computable data in a table, that could already exist as basic data in that table as @Dougie pointed out.

You are trying to do an update, so that is a bit tricky. I think this will do the trick:

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM