2016-11-10 102 views
4

我當前的SQL查詢需要近6個月,並選擇銷售,像這樣有沒有辦法在選擇查詢中動態生成SQL列?

SUM(CASE WHEN ci.InvoiceDate >= '20160501' AND ci.InvoiceDate <= '20160531' THEN cid.QuantityOrdered ELSE 0 END) AS 'May', 
    SUM(CASE WHEN ci.InvoiceDate >= '20160601' AND ci.InvoiceDate <= '20160630' THEN cid.QuantityOrdered ELSE 0 END) AS 'June', 
    SUM(CASE WHEN ci.InvoiceDate >= '20160701' AND ci.InvoiceDate <= '20160731' THEN cid.QuantityOrdered ELSE 0 END) AS 'July', 
    SUM(CASE WHEN ci.InvoiceDate >= '20160801' AND ci.InvoiceDate <= '20160831' THEN cid.QuantityOrdered ELSE 0 END) AS 'August', 
    SUM(CASE WHEN ci.InvoiceDate >= '20160901' AND ci.InvoiceDate <= '20160930' THEN cid.QuantityOrdered ELSE 0 END) AS 'September', 
    SUM(CASE WHEN ci.InvoiceDate >= '20161001' AND ci.InvoiceDate <= '20161030' THEN cid.QuantityOrdered ELSE 0 END) AS 'October', 

然而,當我在十二月運行此報告,因爲我已經在這個月的名稱硬編碼,我不會得到11月的銷售數字。

理想情況下,我希望某種形式的代碼能夠動態生成最近6個月的select查詢。

這甚至可能嗎?有沒有人可以指點我的文檔來學習更多關於動態SQL的知識?

謝謝大家!

+0

你需要這些列?或將行工作? – Amit

+0

不,絕對需要它們作爲列 –

+1

你想要一個[dynamic pivot](http://stackoverflow.com/questions/10404348),這在SQL Server中是可能的,但可能很麻煩。如果您可以在客戶端工具(報表,表單,網頁等)中執行此操作,則用於動態樞軸的UI工具通常會更乾淨,更容易。 –

回答

0

或許這將讓你開始

Declare @Date Date = '2016-10-01' 
Declare @Months int = 6 

Declare @SQL varchar(max)='' 
;with cte0(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)) 
    , cteD(D) As (Select DateAdd(MM,1-Row_Number() over (Order By (Select NULL)),@Date) From cte0 N1, cte0 N2) 
    , cteDate as (Select Top (@Months) DateR1=D,DateR2=DateAdd(DD,-1,DateAdd(MM,1,D)),Title=DateName(MM,D) From cteD) 
Select @[email protected]+','+QuoteName(Title)+'=sum(case when ci.InvoiceDate Between '''+CONVERT(char(10), DateR1,126) +''' and '''+CONVERT(char(10), DateR2,126)+''' Then cid.QuantityOrdered Else 0 End))'+char(13) 
From (Select Top (@Months) * From cteDate Order by DateR1) A 

Select @SQL = 'Select '+Stuff(@SQL,1,1,'')+' From YourTable ' 
Exec(@SQL) 

生成以下SQL

Select [May]=sum(case when ci.InvoiceDate Between '2016-05-01' and '2016-05-31' Then cid.QuantityOrdered Else 0 End)) 
,[June]=sum(case when ci.InvoiceDate Between '2016-06-01' and '2016-06-30' Then cid.QuantityOrdered Else 0 End)) 
,[July]=sum(case when ci.InvoiceDate Between '2016-07-01' and '2016-07-31' Then cid.QuantityOrdered Else 0 End)) 
,[August]=sum(case when ci.InvoiceDate Between '2016-08-01' and '2016-08-31' Then cid.QuantityOrdered Else 0 End)) 
,[September]=sum(case when ci.InvoiceDate Between '2016-09-01' and '2016-09-30' Then cid.QuantityOrdered Else 0 End)) 
,[October]=sum(case when ci.InvoiceDate Between '2016-10-01' and '2016-10-31' Then cid.QuantityOrdered Else 0 End)) 
From YourTable 
+0

'EOMONTH'和'Format'未被識別爲函數? –

+0

@BrendanGooden 2008年更新 –

+0

@BrendanGooden刪除格式()以及 –

0

取得最近6個月的名稱和使用支點構建動態查詢:

declare @months nvarchar(max) = '' 

;with cte as 
(select DATENAME(month, dateadd(MONTH, 0, getdate())) mn, 0 v 
union all 
select DATENAME(month, dateadd(MONTH, v - 1, getdate())) mn, v - 1 
from cte 
where v > -5 
) 
select @months = @months + '[' + mn + '], ' 
from cte 

set @months = SUBSTRING(@months, 1, LEN(@months) - 1) 

declare @q nvarchar(max) 

set @q = ' 
select * 
from 
(
select QuantityOrdered qo 
, DATENAME(month, InvoiceDate) mn 
from invoicetable 
) as s 
pivot 
(sum(qo) for mn in (' + @months + ') 
) p' 

exec(@q) 
相關問題