寻找每日明日总销售额的总计

时间:2012-12-13 11:34:38

标签: sql sql-server tsql pivot

在行中我有几个月。在列中我有1到31天。 我想在一个月的31天内在所有促销活动的末尾添加一列。

SELECT * FROM (
    SELECT DATENAME(month, date) AS SaleMonth,
        DATEPART(dd,date) AS SaleDay FROM EnquiryMaster
    ) p PIVOT 
    (COUNT (SaleDay) FOR 
    SaleDay IN ( [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
                [11],[12],[13],[14],[15],[16],[17],[18],[19],[20],
                [21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31] )) AS pvt

此代码显示31天的日期数据,但不显示总和。

3 个答案:

答案 0 :(得分:2)

如果您希望将此列作为按月显示总数的列,那么遗憾的是,这并非易事。你可以使用这样的东西:

SELECT SaleMonth,
   [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
                [11],[12],[13],[14],[15],[16],[17],[18],[19],[20],
                [21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],

   ([1]+[2]+[3]+[4]+[5]+[6]+[7]+[8]+[9]+[10]+
                [11]+[12]+[13]+[14]+[15]+[16]+[17]+[18]+[19]+[20]+
                [21]+[22]+[23]+[24]+[25]+[26]+[27]+[28]+[29]+[30]+[31]) TotalMonth
FROM 
(
  SELECT DATENAME(month, date) AS SaleMonth,
    DATEPART(dd,date) AS SaleDay 
  FROM EnquiryMaster
) p 
PIVOT 
(
  COUNT (SaleDay) 
  FOR SaleDay IN ( [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
                [11],[12],[13],[14],[15],[16],[17],[18],[19],[20],
                [21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31] )
) AS pvt

请参阅SQL Fiddle with Demo

这可能是动态sql更容易实现来转动数据的情况。通过使用动态SQL,您不必对值进行硬编码。

如果你使用动态sql,你的查询将类似于:

DECLARE @cols AS NVARCHAR(MAX),
    @colsTotal AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(DATEPART(dd,date)) 
                    from EnquiryMaster
                    group by DATEPART(dd,date)
                    order by DATEPART(dd,date)
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

select @colsTotal = STUFF((SELECT distinct '+' + QUOTENAME(DATEPART(dd,date)) 
                    from EnquiryMaster
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')



set @query = 'SELECT SaleMonth,' + @cols + ', '+ @colsTotal+' as GrandTotal from 
             (
                SELECT DATENAME(month, date) AS SaleMonth,
                  DATEPART(dd,date) AS SaleDay 
                FROM EnquiryMaster
            ) x
            pivot 
            (
                count(SaleDay)
                for SaleDay in (' + @cols + ')
            ) p '

execute(@query)

请参阅SQL Fiddle with Demo

或者其他建议,我可能会将您的列标题替换为Month,然后您可以在每个月实施GROUP BY with ROLLUP以获得总计行。

所以你的新结果集看起来像这样:

Day   | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
-------------------------------------------------------------------------------
1     |  12 | 34  | 5   |  3  | 3   |  2  | 32  |  0  | 9   | 87  | 23  | 54
Total |   ....

答案 1 :(得分:0)

尝试BETWEEN条件

SELECT * FROM (SELECT DATENAME(month, date) AS SaleMonth,DATEPART(dd,date) AS SaleDay FROM EnquiryMaster) p PIVOT (COUNT (SaleDay) FOR SaleDay BETWEEN 1 and 31) AS pvt

答案 2 :(得分:0)

尝试在子选择中再添加一列:

SELECT * FROM (
    SELECT DATENAME(month, date) AS SaleMonth,
        DATEPART(dd,date) AS SaleDay,
        COUNT(*) OVER (PARTITION BY DATENAME(month, date)) AS MonthlyTotal
        FROM EnquiryMaster
    ) p PIVOT 
    (COUNT (SaleDay) FOR 
    SaleDay IN ( [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
                [11],[12],[13],[14],[15],[16],[17],[18],[19],[20],
                [21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31] )) AS pvt