3

我有以下代码,但我无法弄清楚如何不将它们包含在 group by 中。有些论点纯粹是针对这种情况的,仅此而已。我不能将它们包含在 group by 中。我真的不能按其他任何东西分组,因为我只需要按 TransactionTyp 获取计数,但我不断收到此错误:在选择列表中无效,因为它既不包含在聚合函数或 GROUP BY 子句中。

也许使用 CASE 是错误的方法?但是我需要根据其他字段替换一些值。

这也在 MS SQL 中。

SELECT  Count(*), 
    CASE 
        WHEN a.TransactionTyp IS NOT NULL THEN a.TransactionTyp
        WHEN a.ClaimStatus = 'Resolved-Deflected' THEN 'Deflected'
        WHEN a.ClaimStatus = 'Resolved-NoAction' THEN 'Deflected'
        WHEN a.ClaimStatus = 'Open' AND b.AssignOperator = 'PendClaim.NF_POS_Pinless' THEN 'Affiliate'
    END As TransactionTyp
FROM Table1 a
LEFT JOIN Table2 b
    ON a.ClaimNbr = b.ClaimDisputeNbr AND b.CreateDte = Convert(varchar,GetDate(), 101)
WHERE a.ClaimEntryDte = Convert(varchar,GetDate(),101)    
AND a.ClaimTypCd  IN ('DEBS', 'DEBSI', 'DBCIN', 'DBCUS')
GROUP BY TransactionTyp
4

4 回答 4

3

一个问题可能是您不能在group by子句中引用别名。尝试重复以下case定义group by

GROUP BY
    CASE 
        WHEN a.TransactionTyp IS NOT NULL THEN a.TransactionTyp
        WHEN a.ClaimStatus = 'Resolved-Deflected' THEN 'Deflected'
        WHEN a.ClaimStatus = 'Resolved-NoAction' THEN 'Deflected'
        WHEN a.ClaimStatus = 'Open' AND b.AssignOperator = 'PendClaim.NF_POS_Pinless' THEN 'Affiliate'
    END As TransactionTyp

或者,使用子查询来定义别名:

select  TransactionTyp
,       count(*)
from    (
        SELECT  CASE 
                WHEN a.TransactionTyp IS NOT NULL THEN a.TransactionTyp
                WHEN a.ClaimStatus = 'Resolved-Deflected' THEN 'Deflected'
                WHEN a.ClaimStatus = 'Resolved-NoAction' THEN 'Deflected'
                WHEN a.ClaimStatus = 'Open' AND b.AssignOperator = 'PendClaim.NF_POS_Pinless' THEN 'Affiliate'
                END As TransactionTyp
        FROM    Table1 a
        LEFT JOIN 
                Table2 b
        ON      a.ClaimNbr = b.ClaimDisputeNbr 
                AND b.CreateDte = Convert(varchar,GetDate(), 101)
        WHERE   a.ClaimEntryDte = Convert(varchar,GetDate(),101)    
                AND a.ClaimTypCd  IN ('DEBS', 'DEBSI', 'DBCIN', 'DBCUS')
        )
GROUP BY 
        TransactionTyp
于 2013-04-26T12:24:31.950 回答
0

CASE必须在Group BY语句中,而不是 ID 。

像这样:

GROUP BY CASE 
    WHEN a.TransactionTyp IS NOT NULL THEN a.TransactionTyp
    WHEN a.ClaimStatus = 'Resolved-Deflected' THEN 'Deflected'
    WHEN a.ClaimStatus = 'Resolved-NoAction' THEN 'Deflected'
    WHEN a.ClaimStatus = 'Open' AND b.AssignOperator = 'PendClaim.NF_POS_Pinless' THEN 'Affiliate'
END
于 2013-04-26T12:24:54.633 回答
0

当您开始指定要从查询返回的内容时,结果已经被分组,因此您想要返回的任何内容都必须在group by.

解决方案是创建一个计算值的子查询,然后您可以对子查询的结果进行分组:

SELECT Count(*), TransactionTyp
FROM (
  SELECT
    CASE 
        WHEN a.TransactionTyp IS NOT NULL THEN a.TransactionTyp
        WHEN a.ClaimStatus = 'Resolved-Deflected' THEN 'Deflected'
        WHEN a.ClaimStatus = 'Resolved-NoAction' THEN 'Deflected'
        WHEN a.ClaimStatus = 'Open' AND b.AssignOperator = 'PendClaim.NF_POS_Pinless' THEN 'Affiliate'
    END As TransactionTyp
  FROM Table1 a
  LEFT JOIN Table2 b
    ON a.ClaimNbr = b.ClaimDisputeNbr AND b.CreateDte = Convert(varchar,GetDate(), 101)
  WHERE a.ClaimEntryDte = Convert(varchar,GetDate(),101)    
  AND a.ClaimTypCd  IN ('DEBS', 'DEBSI', 'DBCIN', 'DBCUS')
) as x
GROUP BY TransactionTyp
于 2013-04-26T12:26:11.687 回答
0

无需通过如何获取当前日期结果或当前月份结果在组上添加 create_on

select segment_name, 
cast(SUM(case when t1.create_on=GETDATE()) then actual_total_sale    as currentDateData,
cast(SUM(case when MONTH(create_on) = MONTH(getDate()) then actual_total_sale    as currentMonthData,
 from 
PR_DAILY_AOP_TARGET t1
where is_active=1 group by segment_name
于 2021-02-12T08:07:28.250 回答