0

我有一个包含以下记录的表:

date, name, amount, balance, debit_credit. 

这张表代表了在一段时间内对几家公司进行观察的样本。

我想用以下记录制作下表:

aggregate1, aggregate2, aggregate3, aggregate4, name.

在哪里:

aggregate1 - sum of amount for all names where balance equals 1500, 1501;
aggregate2 - sum of amount for all names where balance equals 1400, 1401;
aggregate3 - sum of amount for all names where balance equals 1300, 1301;

我编写了以下 SQL 查询来计算聚合 1。

SELECT Sum(Firms.amount)/100 AS [aggregate1], Firms.name
FROM Firms
where Firms.Balance in (1500, 1502)
GROUP BY Firms.name;

但是,我很好奇如何将不同 where 条件的查询表(见上文)中的输出组合起来?此外,我想知道,如何将日期设置为该查询的参数?

我会感谢您的建议和解决方案。谢谢您的考虑。

4

1 回答 1

2

也许是这样的:

SELECT
    Firms.name,
    SUM(iif(Firms.Balance IN (1500, 1502),Firms.amount,0))/100 AS aggregate1,
    SUM(iif(Firms.Balance IN (1400, 1401),Firms.amount,0))/100 AS aggregate2,
    SUM(iif(Firms.Balance IN (1300, 1301),Firms.amount,0))/100 AS aggregate3
FROM Firms
WHERE Firms.date >= #12/6/2012 3:54:15 PM#  
GROUP BY Firms.name;

参考:

于 2013-10-16T07:53:56.947 回答