2015-06-30 169 views
4

我想顯示一個表格,其中月份是垂直的,年份是水平的,我給出一個來自下拉列表的年份和月份的輸入。 我的預期產出低於:月份和年份明智的報告

 2011 2012 2013 2014 
jan 1000 1500 5000 1000 
feb 00 00 2000 2000 
mar . 
.  . 
.  . 
dec . 

我的查詢是

select 
    datepart(year, DateOfTransaction), 
    left(datepart(month, DateOfTransaction), 3), 
    sum(amount) 
from TBL_Transactionmaster 
where 
    datepart(year, DateOfTransaction) = 'input year' 
    and datepart(month, DateOfTransaction) = 'input month' 
+1

您可以使用數據透視這一點。 –

+0

yep.But月是橫向顯示 –

回答

5

嘗試此查詢。

靜態樞軸

SELECT * 
    FROM (
     SELECT 
      left(datename(month,DateOfTransaction),3)as [month], year(DateOfTransaction) as [year] 
      , Amount 
     FROM TBL_Transactionmaster 
    ) as s 
    PIVOT 
    (
     SUM(Amount) 
     FOR [Year] in([2011],[2012],[2013],[2014],[2015]) 
    )AS piv 

對於動態透視

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

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(year(DateOfTransaction)) 
        from TBL_Transactionmaster 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 

set @query = 'SELECT [Month],' + @cols + ' from 
      (
       SELECT 
      left(datename(month,DateOfTransaction),3)as [month], year(DateOfTransaction) as [year] 
      , Amount 
     FROM TBL_Transactionmaster 
      ) x 
      pivot 
      (
       sum(amount) 
       for [year] in (' + @cols + ') 
      ) p ' 

execute(@query) 

**對於所有月份和替換空0 **

SELECT [month], Isnull([2011],0) as [2011] , ISnull([2012],0) as [2012] ,ISNULL ([2013],0) as [2013] , ISNULL([2014],0) as [2014] , ISNULL([2015],0) as [2015] 
    FROM (
     SELECT 
      left(datename(month,DateOfTransaction),3)as [month], Amount, year(DateOfTransaction) as [year] 

     FROM TBL_Transactionmaster 
     UNION ALL 
    select [MONTH], Amount, [year] FROM 
    (Select 'Jan' as [Month] , 0 as Amount, year(Getdate()) as [year] Union ALL 
    Select 'Feb' as [Month] , 0 as Amount, year(Getdate()) as [year] Union ALL 
     Select 'Mar' as [Month] , 0 as Amount, year(Getdate()) as [year] Union ALL 
     Select 'Apr' as [Month] , 0 as Amount, year(Getdate()) as [year] Union ALL 
     Select 'May' as [Month] , 0 as Amount, year(Getdate()) as [year] Union ALL 
     Select 'Jun' as [Month] , 0 as Amount, year(Getdate()) as [year] Union ALL 
      Select 'Jul' as [Month] , 0 as Amount, year(Getdate()) as [year] Union ALL 
      Select 'Aug' as [Month] , 0 as Amount, year(Getdate()) as [year] Union ALL 
      Select 'Sep' as [Month] , 0 as Amount, year(Getdate()) as [year] Union ALL 
      Select 'Oct' as [Month] , 0 as Amount, year(Getdate()) as [year] Union ALL 
       Select 'Nov' as [Month] , 0 as Amount, year(Getdate()) as [year] Union ALL 
       Select 'Dec' as [Month] , 0 as Amount, year(Getdate()) as [year]) MN 

    ) as s 
    PIVOT 
    (
     SUM(Amount) 
     FOR [Year] in([2011],[2012],[2013],[2014],[2015]) 
    )AS piv 
+0

嗨阿倫,Static很好,但是當年有人交易,那麼怎麼會顯示它。我所有的交易年表都是動態顯示 –

+0

只需加上'[2011],[2012 ],[2013],[2014],[2015]靜態查詢。檢查我更新的答案。 –

+0

對於動態查詢,我在哪裏放置了我的輸入年份和月份 –

2

您可以使用像這樣的查詢:

SELECT DATENAME(MONTH, DateOfTransaction) As [Month] 
    , SUM(CASE WHEN DATEPART(YEAR, DateOfTransaction) = 2011 THEN amount ELSE 0 END) AS [2011] 
    , SUM(CASE WHEN DATEPART(YEAR, DateOfTransaction) = 2012 THEN amount ELSE 0 END) AS [2012] 
    , SUM(CASE WHEN DATEPART(YEAR, DateOfTransaction) = 2013 THEN amount ELSE 0 END) AS [2013] 
    , SUM(CASE WHEN DATEPART(YEAR, DateOfTransaction) = 2014 THEN amount ELSE 0 END) AS [2014] 
FROM TBL_Transactionmaster 
GROUP BY DATENAME(MONTH, DateOfTransaction), DATEPART(MONTH, DateOfTransaction) 
ORDER BY DATEPART(MONTH, DateOfTransaction) 

您可以使用動態SQL這樣的:

DECLARE @sql nvarchar(max); 

SELECT @sql = ISNULL(@sql, 'DATENAME(MONTH, DateOfTransaction) As [Month]') + ', SUM(CASE WHEN DATEPART(YEAR, DateOfTransaction) = ' + 
       CAST(DATEPART(YEAR, DateOfTransaction) AS VARCHAR(5)) + ' THEN amount ELSE 0 END) AS [' + CAST(DATEPART(YEAR, DateOfTransaction) AS varchar(5)) + ']' 
FROM TBL_Transactionmaster 
GROUP BY DATEPART(YEAR, DateOfTransaction) 
ORDER BY DATEPART(YEAR, DateOfTransaction); 

SET @sql = 'SELECT ' + @sql + ' FROM TBL_Transactionmaster GROUP BY DATENAME(MONTH, DateOfTransaction), DATEPART(MONTH, DateOfTransaction) ORDER BY DATEPART(MONTH, DateOfTransaction)'; 

EXEC(@sql); 
+0

靜態是好的,但有人在本年交易,那麼怎麼會顯示它。我所有的交易年表都是動態顯示 –

+1

@ManoJohnbritto您可以使用動態SQL;)。 –