2016-04-14 76 views
1
SELECT Count (*) AS Amount FROM Tbl_Original WHERE AcctNumber IS NOT NULL AND dateadded between '20151001' AND '20151031' 
UNION 
SELECT Count (*) FROM Tbl_Original WHERE AcctNumber IS NOT NULL AND dateadded between '20151101' AND '20151130' 
UNION 
SELECT Count (*) FROM Tbl_Original WHERE AcctNumber IS NOT NULL AND dateadded between '20151201' AND '20151231'` 

Tbl_OriginalSQL代碼按日期

AcctNu  Dateadded 
----------------------- 
qwe56755 10/22/2015 
qwe33455 10/06/2015 
qwe09967 11/17/2015 

試圖讓這個:

Month  Amount 
------------------- 
October 2 
November 1 

使用此SQL(在Access)。

回答

0

您需要爲此類查詢提供恆定的月份名稱。

SELECT 'October' AS Month, Count (*) AS Amount FROM Tbl_Original WHERE AcctNumber IS NOT NULL AND dateadded between '20151001' AND '20151031' 
UNION 
SELECT 'November' AS Month, Count (*) AS Amount FROM Tbl_Original WHERE AcctNumber IS NOT NULL AND dateadded between '20151101' AND '20151130' 
UNION 
SELECT 'December' AS Month, Count (*) AS Amount FROM Tbl_Original WHERE AcctNumber IS NOT NULL AND dateadded between '20151201' AND '20151231' 

雖然交叉表查詢的基礎上,從日期字段提取一個月的查詢時,可能會更適合你想要的東西。

+0

謝謝安德烈!這工作得很好 – onemoremile

+0

試圖做交叉表(我想,但我可能是錯的)的問題是我正在使用繼承的數據庫。有一些非常糟糕的桌子設計,我必須解決。例如,「dateadded」字段被編碼爲「文本」而不​​是「日期」,並且當然它被鏈接到其他表和查詢。所以我爲給定的範圍分配了幾個月,然後以某種方式顯示數據。再次感謝 – onemoremile

+0

@onemoremile:如果答案解決了您的問題,您可以[接受它](http://stackoverflow.com/help/someone-answers),這也標誌着問題已解決。 – Andre