我有一個關於SQL Server的問題。請告訴我如何解決在SQL Server這個問題沒有銷售日期在SQL Server中顯示爲零
我有兩列sale_date
和amount
表:
sale_date amount
-------------------
05/01/2014 400
05/02/2014 350
05/04/2014 430
.
.
,但我所要的輸出包括amount
零值,如果沒有特定月份的銷售額。
像這樣:
sale_date amount
-------------------
05/01/2014 400
05/02/2014 350
05/03/2014 0
05/04/2014 430
.
.
請告訴我如何在SQL Server解決這個
一種方法是使用數字表或日曆表:http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html建議Sarath Avanavu的解決方案使用遞歸CTE即時生成這樣的表格。有很多方法可以生成一個數字表格:http://sqlperformance.com/2013/01/t-sql-queries/generate-a-set-1 – 2015-01-21 06:32:40