2013-10-05 45 views
1

我對此查詢有絕對的大腦鎖定,並且想知道是否有人可以幫助我。要將使用公共組的兩個彙總查詢合併到MS Access中的一個查詢中

我在1毫秒的訪問表中有以下格式的日常數據: clients(unique)|日期| openingbal | closingbal | 我有一個SQL來得到沒有。的每日客戶餘額收盤價高於開盤價(漲幅)

SELECT Dates, Count(closingbal) AS Increases FROM tablename 
WHERE closingbal >[ openingbal] GROUP BY Dates 

這個工作正常。

我也有一個客戶餘額指收價比開盤價(減少)低:

SELECT Dates, Count(closingbal) AS Decreases FROM tablename 
WHERE closingbal <[ openingbal]GROUP BY Dates 

這工作也沒關係。 我完全陷入瞭如何在形式這兩個查詢合併爲一個查詢:

Date | Increases | Decreases | 
2013-08-22 | 1748 | 743 | 
2013-08-23 | 1145 | 112 | 
2013-08-24 | 1640 | 912 | 

與此查詢的任何援助表示高度讚賞。

回答

2

怎麼回合使用聯盟

Select Dates, Sum(Increases), Sum(Decreases) 
from 
{ 
    SELECT Dates, Count(closingbal) AS Increases, 0 as Decreases FROM tablename 
    WHERE closingbal >[ openingbal] GROUP BY Dates 

    Union All 

    SELECT Dates, 0 as Increase, Count(closingbal) AS Decreases FROM tablename 
    WHERE closingbal <[ openingbal]GROUP BY Dates 
} 
GroupBy Dates 
+0

這工作了,這樣的快速反應。非常感謝你,非常感謝! – Avagut

+0

不客氣,玩編碼:D –