我有以下代码,我无法弄清楚如何不将这些包含在组中。一些论据纯粹是针对这种情况而已。我不能把它们包含在小组中。由于我只需要通过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
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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
答案 3 :(得分: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