按一列的总和与另一列的一般值分组的错误

Error grouping by the sum of one column to the general value of the other

我有一个参数列表,第一个是日期范围@start_dt 和@end_dt 我的字段之一是模式。如果模式 = 1,查询是 运行 一种方式,如果模式 - 2,另一种方式,等等。我有 7 种可能的版本。

当mode = 6时,我想说的是 拉出下方日期范围列表之间的所有列,其中一列 pmt_rcvd_amt 不等于 sli_paid_amt 的总和。现在我想要 sli_paid_amt 按 customer_no

分组 Where a.create_dt between @start_dt and @end_dt and a.pmt_rcvd_amt <> sum(a.sli_paid_amt)

例如:

customer_no sli_due_amt pmt_rcvd_amt`
85244305    200.00  200.00
74500386    50.00   219.00
74500386    219.00  219.00
74500386    10.00   219.00
86119821    NULL    NULL

我希望它对 customer_no 74500386
进行分组(对所有 sli_due_amt 求和)所以 219 + 50. + 10 因此 279 然后将其与pmt_rcvd_amt 列中的 219。 (这些值将相同)

我的代码是这样的……

select distinct 
        a.id_key ,
        a.customer_no ,
        a.customer_prefix ,
        a.customer_lname 
        ... 
        a.current_child_price ,
        a.current_other_price 
        from LV_CHC_TOURS_RSV_DATA a
        left outer join LT_CHC_TOURS_RSV_LANG b on a.language = b.id 
        left outer join LV_CHC_TOURS_RSV_CS c on a.add_text = c.source_no 
        Where a.create_dt between @start_dt and @end_dt 
        and a.pmt_rcvd_amt <> sum(a.sli_paid_amt)

Error Message: An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

然后我将其更改为尝试在我的代码中使用 having:

from LV_CHC_TOURS_RSV_DATA a
        left outer join LT_CHC_TOURS_RSV_LANG b on a.language = b.id 
        left outer join LV_CHC_TOURS_RSV_CS c on a.add_text = c.source_no 
        Where a.create_dt between @start_dt and @end_dt 
        group by a.customer_no
        having sum(a.sli_paid_amt) <> a.pmt_rcvd_amt

现在我收到以下错误消息:

Column 'LV_CHC_TOURS_RSV_DATA.pmt_rcvd_amt' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.

我知道这有点奇怪 - 但我希望给定 customer_no 对所有 sli_due_amt 求和,然后查看它们是否等于 pmt_rcvd_amt 的不同值。因为那些不同的值在每一行中都是相同的。

我想要的输出是:

customer_no sli_due_amt pmt_rcvd_amt`
74500386    50.00   219.00
74500386    219.00  219.00
74500386    10.00   219.00

因为:50 + 219 + 10 不等于 219(按客户 74500386 分组)

HAVING 子句中你只能有聚合函数。 a.pmt_rcvd_amt 每行都相同,因此您可以使用 min(a.pmt_rcvd_amt)max(a.pmt_rcvd_amt)
在下面的代码子查询 returns 中满足您要求的所有客户的数量。然后它们被主查询用于 select 所有需要的行。

所以试试这个:

select 
    a.id_key ,
    a.customer_no ,
    a.customer_prefix ,
    a.customer_lname 
    ... 
    a.current_child_price ,
    a.current_other_price 
from LV_CHC_TOURS_RSV_DATA a 
    left outer join LT_CHC_TOURS_RSV_LANG b on a.language = b.id 
    left outer join LV_CHC_TOURS_RSV_CS c on a.add_text = c.source_no 
where a.customer_no in 
( select a1.customer_no from LV_CHC_TOURS_RSV_DATA a1       
    Where a1.create_dt between @start_dt and @end_dt 
    group by a1.customer_no
    having sum(a1.sli_paid_amt) <> min(a1.pmt_rcvd_amt))
SELECT a.customer_no, SUM(a.sli_paid_amt), MAX(a.pmt_rcvd_amt) AS [Payment Amount], MAX(some_other_field), ...
 from LV_CHC_TOURS_RSV_DATA a
        left outer join LT_CHC_TOURS_RSV_LANG b on a.language = b.id 
        left outer join LV_CHC_TOURS_RSV_CS c on a.add_text = c.source_no 
        Where a.create_dt between @start_dt and @end_dt 
        group by a.customer_no
        having sum(a.sli_paid_amt) <> [Payment Amount]

如果该字段不在分组依据中,则它必须是聚合函数,例如 SUM/MAX 等...如果您按客户 ID 分组,则上述查询应该有效。然后只需用 MAX(-) 将 select 语句中的字段包装起来,因为 MAX 也可以与 char 字段一起使用。

如果我没理解错的话,首先你需要为每个客户总结 sli_due_amt,然后你想要 return 个别行,不包括 pmt_rcvd_amt 的客户= 求和 sli_due_amt.

所以:

WITH Summed as
(select
customer_no,
sum(sli_due_amt) as sli_sum
from LV_CHC_TOURS_RSV_DATA...
GROUP BY cust_no
)

    select *
    from
    LV_CHC_TOURS_RSV_DATA t1
    inner join summed
      on t1.customer_no = summed.customer_no
      and t1.pmt_rcvd_amt <> sli_sum

当然,我严重过度简化了您的查询。