2012-12-13 95 views
1

我有幾個月的排。在我有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天的明智數據,但不顯示總和。

回答

1

如果你想這是一列顯示由一個月總有不幸的是,沒有簡單的方法。您可以使用這樣的事情:

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

嘗試狀況調查

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