使用 SQL Server 2008:我正在尝试编写过去一周发生的超过 1000 美元的客户交易报告。我有以下 SQL 查询,它将为我提供正确的记录,但显然汇总了结果:
SELECT
customerID,
CAST(createdAt AS DATE) AS transactionDate,
SUM(transactionAmount) as dailyTotal,
FROM transactions
WHERE createdAt > DATEADD( DAY, -7, GETDATE() )
GROUP BY clientID, CAST(createdAt AS DATE)
HAVING SUM(transactionAmount) > 1000
导致类似:
| customerID | transactionDate | dailyTotal |
| 1 | 2013-11-01 | 1212 |
| 2 | 2013-11-01 | 10002 |
...
| 1 | 2013-11-02 | 5212 |
但是,我需要获取包含这些汇总结果的各个记录,但显然不能省略该GROUP BY
语句。也许我想要实现的目标在单个查询中是不可能的?