sql-server – 获取金额记录的总和,因为每行是月份,列是星期是使用sql?

表TBL_yearlyReport中有1000条记录,有2列:

> Date_Val(例如’2016-12-25 23:48:40.360′)
>金额(例如100,1000,100等)

TBL_yearlyReport中的示例数据

Date_Val    | Amount 
---------------------
2016-12-25  | 20000
2016-02-21  |  2000

我希望看到一年的报告,我希望在2012年过去一年等

报告每个月必须有一行,返回的列将是4,即week1,week2,week3,week4

每周的列值是当前周量的总和

这是我到目前为止所做的:

DECLARE @LoopCounter INT = 1
DECLARE @MONTH INT = 1

BEGIN
     WHILE(@LoopCounter <= 100)
     BEGIN
         SET @LoopCounter  = @LoopCounter  + 1                 

         IF @MONTH = 1 
         BEGIN
             SELECT SUM(convert(INT, amount)) as JAN,  
                    @MONTH AS month  
             FROM TBL_yearlyReport 
             WHERE YEAR(Date_Val) = 2016 AND @MONTH = @MONTH 
         END

         IF @MONTH = 2 
         BEGIN
             SELECT SUM(convert(INT, amount)) as FEB,  
                    @MONTH AS month  
             FROM TBL_yearlyReport 
             WHERE YEAR(Date_Val) = 2016 AND @MONTH = @MONTH 
         END 

                 if @MONTH = 3 
                       begin
          SELECT sum(convert(int ,amount)) as FEB ,  @MONTH as month  FROM TBL_yearlyReport 
                    WHERE  YEAR(Date_Val) = 2016 and  @MONTH = @MONTH 
                end

             /////////so on continue to

                     if @MONTH = 12
                       begin
          SELECT sum(convert(int ,amount)) as FEB ,  @MONTH as month  FROM TBL_yearlyReport 
                    WHERE  YEAR(Date_Val) = 2016 and  @MONTH = @MONTH                     
                end    

                if @MONTH = 12
                begin
                   break
                end  
                 SET @MONTH   = @MONTH  + 1  
                end    END

现在的问题是如何获得月份之间的所有ID,以便使用BETWEEN来总和金额?

如何获得每个月每周的金额总和

     week1   |    week2   |  week3   | week4

      jan  
      feb
      march 
      june

等等

如果我有他们的ID,我想这样做是为了每周获得金额的总和

SELECT SUM(convert(INT, amount)) ah  
FROM TBL_yearlyReport 
WHERE YEAR(Date_Val) = 2016  
  AND id between 1 and 100     

输出应该是:

SELECT @WEEK1, @WEEK2, @WEEK3, @WEEK4 
FROM TBL_yearlyReport 
WHERE Date_Val BETWEEN @startWEEKDATE AND @EndWEEKDATE

最佳答案 我想几个星期的计算需要这样:

《sql-server – 获取金额记录的总和,因为每行是月份,列是星期是使用sql?》

我们可以把周数比作11,除以4得到2,75.然后我们拿75,看到它等于第3周.通过这种方式,我们可以转换所有周数.
然后你可以使用cte pivot:

;WITH cte AS (
SELECT MONTH(Date_Val) as [MonthNum],
       DATENAME(month,Date_Val) as [Month],
       DATEPART(week,Date_Val) as [ActualWeekNum],
       FLOOR((CAST(DATEPART(week,Date_Val) as decimal(5,2))/4- FLOOR(CAST(DATEPART(week,Date_Val) as decimal(5,2))/4))*100) as [Week],
       Amount as [Amount]
FROM  TBL_yearlyReport
WHERE  YEAR(Date_Val) = 2012
)

SELECT [Month],Week1,Week2,Week3,Week4
FROM
(
SELECT [MonthNum],
       [Month],
       CASE WHEN [Week] = 25 THEN 'Week1' 
            WHEN [Week] = 50 THEN 'Week2'
            WHEN [Week] = 75 THEN 'Week3' 
            WHEN [Week] = 0  THEN 'Week4'
            ELSE NULL
            END as [Week],
       Amount 
FROM cte
) d
pivot
(
SUM(Amount) for [WEEK] in (Week1,Week2,Week3,Week4)
) piv
ORDER BY [MonthNum];

结果是:

    Month                          Week1       Week2       Week3       Week4
------------------------------ ----------- ----------- ----------- -----------
January                        1995        4798        5280        4500
February                       6102        4283        4039        4519
March                          7445        2616        5701        5977
April                          4991        5624        4823        4901
May                            3826        5858        1970        3703
June                           3461        3950        4208        1707
July                           2970        2469        5217        2861
August                         2536        2535        4887        2210
September                      2352        2721        4232        3165
October                        2876        3044        4702        7160
November                       3183        3158        4509        5507
December                       3666        3509        3276        3257
点赞