在sql中求和时重复行

时间:2013-07-24 15:53:39

标签: sql

我有以下简单查询,显示valuehomeel2的总和。

如果我在我的查询中添加了另一列(请参阅select语句2),并说只显示valuehome到期日在今天之前,即它已逾期,我在{{1}上丢失了我的总和}因为结果显示每个截止日期的行

如何保持valuehome的总和,以便即使有多个截止日期,我也会看到valuehome的一个结果。

valuehome

谢谢Nenad - 这是很棒的帮助 - 你知道我将如何在where语句中更改我的静态日期

不要向SELECT TOP (100) PERCENT el2, statpay, SUM(valuehome) AS Expr1 FROM dbo.MBO450 GROUP BY el2, statpay HAVING (el2 LIKE 'C%') AND (statpay = 84) ORDER BY el2 SELECT TOP (100) PERCENT el2, statpay, SUM(valuehome) AS Expr1, duedate FROM dbo.MBO450 GROUP BY el2, statpay, duedate HAVING (el2 LIKE 'C%') AND (statpay = 84) AND (duedate <= CONVERT(DATETIME, '2013-07-24 00:00:00', 102)) ORDER BY el2 GROUP BY添加duedate - 在分组前过滤WHERE条件中的行:

HAVING

以便静态日期是今天的日期?

1 个答案:

答案 0 :(得分:0)

不要将duedate添加到GROUP BYHAVING - 在分组之前过滤WHERE条件中的行:

SELECT el2, statpay, SUM(valuehome) AS Expr1
FROM dbo.MBO450
WHERE duedate <= CONVERT(DATETIME, '2013-07-24 00:00:00', 102))
GROUP BY el2, statpay
HAVING (el2 LIKE 'C%') AND (statpay = 84) 
ORDER BY el2

PS:TOP (100) PERCENT无用

PPS:您还可以将el2statpay条件移至WHERE

PPPS:今天替换为WHERE duedate <= GETDATE()动态