1

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

我有 1 ms 访问表中的每日数据,格式为:clients (unique) | 日期 | 开幕式 | 闭幕式 | 我有一个 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 |

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

4

1 回答 1

2

使用 Union 怎么样

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
于 2013-10-05T08:37:42.223 回答