将使用公共组的两个摘要查询合并到MS Access中的一个查询中

时间:2013-10-05 08:31:00

标签: sql ms-access subquery summary

我对此查询有绝对的大脑锁定,并且想知道是否有人可以帮助我。

我在1 ms访问表中的每日数据格式如下: 客户(独特)|日期|开放的| 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 |

非常感谢您对此查询的任何帮助。

1 个答案:

答案 0 :(得分: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