MS SQL Server查询开始日期和结束日期之间每个项目的QUERY SUM

时间:2017-02-12 20:57:19

标签: sql sql-server tsql

我有一张桌子' pay_schedule'看起来像这样:

------------------------------------------------------------------------------
Title (char[50])  | FilmId (int)     payout (Dec(18,2))    pay_date (date)    |
------------------------------------------------------------------------------|
Big Secret        | -2147483648     |   900.00          |   4/9/2017          | 
puzzled           | -2147483631     |   512.50          |   4/9/2017          |
puzzled           | -2147483631     |   325.94          |   4/9/2017          |
puzzled           | -2147483631     |   325.94          |   7/9/2017          |
Star men          | -2147483639     |   512.5           |   7/9/2017          |
Deep beneath      | -2147483636     |   900             |   7/9/2017          |
Deep beneath      | -2147483636     |   900             |   10/9/2017         |
Deep beneath      | -2147483636     |   512.5           |   10/9/2017         |
Deep beneath      | -2147483636     |   325.94          |   10/9/2017         |
puzzled           | -2147483631     |   325.94          |   1/9/2018          |
Star ment         | -2147483639     |   512.5           |   1/9/2018          |
puzzled           | -2147483639     |   900             |   1/9/2018          |
Mirzya            | -2147483639     |   900             |   4/9/2018          |
puzzled           | -2147483639     |   512.5           |   4/9/2018          |
.....             | ..........      |   ......          |   .....             |
                  |                 |                   |                     |

我想得到一个结果:

  • 每个月的每个标题的SUM(支付)
  • 开始日期和结束日期之间每月的总付款额。如下图所示:
   Start Date: 01/01/2017   End Date: 12/31/2019
    -------------------------------------------------------------------------
    Month Y         | Big Secret    |Deep beneath   | puzzled    | star men |
                    |               |               |            |          |
    ------------------------------------------------------------------------|
    apr 2017        |   900.00      |      0.00     |   838.44   |    0.00  |
    jul 2017        |     0.00      |    900.00     |   325.94   |  512.50  |
    oct 2017        |     0.00      |   1738.44     |     0.00   |    0.00  |
    jan 2018        |     0.00      |      0.00     |  1225.94   |  512.50  |
    apr 2018        |   900.00      |               |   512.50   |          |
    _________________________________________________________________________

**不包括付款的月份

我非常感谢您的时间和帮助。由于这是一个大表,如果你指出可能的优化,我将不胜感激。祝你有个美好的一天!

2 个答案:

答案 0 :(得分:2)

如果您将结果集格式化为filemonth,并使用付款,则可以使用汇总:

select title, year(paydate) as yyyy, month(paydate) as mm,
       sum(payout)
from pay_schedule
group by title, year(paydate), month(paydate)
order by title, yyyy, mm;

如果你有很多行,你甚至可能无法根据需要重组数据。这可能意味着许多列和超过一千个左右超过SQL Server限制。

如果您仍想沿着这条路走下去,Google"动态透视SQL Server"。

答案 1 :(得分:0)

试试这个:

    CREATE TABLE #tt(Title CHAR(50),FilmId INT,payout DECIMAL(18,2),pay_date DATE)
    INSERT INTO #tt
    SELECT 'Big Secret',-2147483648,900.00,'4/9/2017' UNION ALL
    SELECT 'puzzled',-2147483631,512.50,'4/9/2017' UNION ALL
    SELECT 'puzzled',-2147483631,325.94,'4/9/2017' UNION ALL
    SELECT 'puzzled',-2147483631,325.94,'7/9/2017' UNION ALL
    SELECT 'Star men',-2147483639,512.5 ,'7/9/2017' UNION ALL
    SELECT 'Deep beneath',-2147483636,900,'7/9/2017' UNION ALL
    SELECT 'Deep beneath',-2147483636,900,'10/9/2017' UNION ALL
    SELECT 'Deep beneath',-2147483636,512.5 ,'10/9/2017' UNION ALL
    SELECT 'Deep beneath',-2147483636,325.94,'10/9/2017' UNION ALL
    SELECT 'puzzled',-2147483631,325.94,'1/9/2018' UNION ALL
    SELECT 'Star ment',-2147483639,512.5 ,'1/9/2018 ' UNION ALL
    SELECT 'puzzled',-2147483639,900,'1/9/2018' UNION ALL
    SELECT 'Mirzya',-2147483639,900,'4/9/2018' UNION ALL
    SELECT 'puzzled',-2147483639,512.5,'4/9/2018 ' 


    SELECT @col1=ISNULL(@col1+',','')+QUOTENAME(RTRIM(Title)),@col2=ISNULL(@col2,'')+',ISNULL('+QUOTENAME(RTRIM(Title))+',0) AS '+QUOTENAME(RTRIM(Title)) FROM #tt GROUP BY RTRIM(Title)
    PRINT @col2
    SET @sql='SELECT LEFT(mon,3)+'' ''+LTRIM(yr) as [Month Y]'+@col2+' FROM ('+CHAR(13)+
    '   SELECT DATENAME(MONTH,pay_date) AS mon ,YEAR(pay_date) AS yr,RTRIM(Title) AS Title,SUM(payout) AS payout FROM #tt'+CHAR(13)+
    '   GROUP BY DATENAME(MONTH,pay_date) ,YEAR(pay_date),RTRIM(Title)'+CHAR(13)+
    ') AS t '+CHAR(13)+
    'PIVOT(MAX(payout) FOR Title IN ('+@col1+')) p'
    EXEC(@sql)
Month Y          Big Secret                              Deep beneath                            Mirzya                                  puzzled                                 Star men                                Star ment
---------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
Apr 2017         900.00                                  0.00                                    0.00                                    838.44                                  0.00                                    0.00
Jul 2017         0.00                                    900.00                                  0.00                                    325.94                                  512.50                                  0.00
Oct 2017         0.00                                    1738.44                                 0.00                                    0.00                                    0.00                                    0.00
Apr 2018         0.00                                    0.00                                    900.00                                  512.50                                  0.00                                    0.00
Jan 2018         0.00                                    0.00                                    0.00                                    1225.94                                 0.00                                    512.50