T-sql :get SUM of Columns

后端 未结 3 581
梦毁少年i
梦毁少年i 2021-01-27 15:56

I have a table that looks something like the following :

          W1  W2 w3
Gold      10    2    3              
Silver     3    1    1

but i

3条回答
  •  囚心锁ツ
    2021-01-27 16:21

    One thought, since you tagged this question with Reporting Services. If, in the end, you are displaying the info using Reporting Services I would highly consider using the Matrix tool to do the pivoting and summation of the data because that is exactly what it does.

    To further explain as it seems you are going to use SSRS. Your matrix would have a dataset that would be similar to this:

    SELECT
       [week]=DATEPART(ISO_WEEK,ta.enddate),
       ta.id,
       ta.MetalType as GoldorSilver
    FROM  table1 ta 
    where ta.enddate BETWEEN '2016/01/01' AND '2016/12/31'
    

    The matrix would have a header and footer and column group would be [Week] with a Column Group total to do the sum across the week. The row group footer would do the sum across all weeks.

提交回复
热议问题