我遇到了一些问题。我想聚合具有相同“STATUS”值的行并计算元素。问题是,“STATUS”列是在嵌套查询中计算的。
查询:
select
(case
when stats.status = 'RJCT' AND stats.status_reason = 'NARR' AND stats.signing_status = 'SIGNED' THEN 'Rejected(error)/Unknown'
when stats.status = 'RJCT' AND stats.status_reason != 'NARR' THEN 'Rejected'
when stats.status = 'PDNG' AND stats.status_reason is null THEN 'Unknown'
when stats.status = 'PDNG' AND stats.status_reason = 'NARR' and stats.signing_status = 'SIGNED' THEN 'onHold/PendingUserConfirmation'
when stats.status = 'PDNG' AND stats.status_reason = 'DS0A' and stats.signing_status = 'SIGNED' THEN 'PartiallyConfirmed'
when stats.status = 'PDNG' AND stats.signing_status = 'PENDING' THEN 'PendingConfirmation'
when stats.status = 'PDNG' AND stats.signing_status = 'SIGNING' THEN 'PendingUserApproval'
when stats.status = 'PDNG' AND stats.signing_status = 'CANCELLED' THEN 'UserApprovalCancelled'
when stats.status = 'PDNG' AND stats.signing_status = 'EXPIRED' THEN 'UserApprovalTimeout'
when stats.status = 'PDNG' AND stats.signing_status = 'FAILED' THEN 'UserApprovalFailed'
when stats.status = 'PDNG' AND stats.signing_status = 'SIGNED' THEN 'Unknown'
when stats.status = 'ACCP' THEN 'Paid'
when stats.status = 'ACSP' AND stats.signing_status = 'PENDING' THEN 'PendingConfirmation'
when stats.status = 'ACSP' AND stats.signing_status = 'SIGNED' THEN 'Confirmed'
when stats.status = 'ACSP' AND stats.signing_status = 'EXPIRED' THEN 'UserApprovalTimeout'
when stats.status = 'ACSP' AND stats.signing_status = 'FAILED' THEN 'UserApprovalFailed'
when stats.status = 'ACWC' AND stats.status_reason = 'NARR' AND stats.signing_status = 'SIGNED' THEN 'Paid'
when stats.status = 'ACWC' AND stats.status_reason = 'NARR' AND stats.signing_status = 'SIGNED' THEN 'Paid'
when stats.status = 'ACWC' AND stats.status_reason != 'NARR' THEN 'PendingConfirmation'
when stats.status = 'EXECUTING' AND stats.signing_status = 'SIGNED' THEN 'Confirmed'
when stats.status = 'EXECUTING' AND stats.signing_status = 'PENDING' THEN 'Confirmed'
when stats.status = 'FAILED' THEN 'UserApprovalFailed'
ELSE 'UNKNOWN' END)
as status, stats.count
from (select status, status_reason, signing_status, count(*) as count from payments
group by status, signing_status, status_reason) stats;
结果:
Unknown 125
onHold/PendingUserConfirmation 15
Confirmed 12
UserApprovalFailed 41
UNKNOWN 22
Rejected(error)/Unknown 2
Rejected 176
Paid 1089
Rejected 72
PendingConfirmation 219
Unknown 7
PendingConfirmation 14835
UserApprovalFailed 10
Rejected 13
UserApprovalTimeout 145
Rejected 2
Paid 2
UNKNOWN 1
UserApprovalFailed 355
Unknown 13
PartiallyConfirmed 301
Confirmed 510
Unknown 2
PendingConfirmation 1
UserApprovalFailed 4
Unknown 13
Unknown 5
Confirmed 2399
Rejected 1
Rejected 5
Confirmed 7
Confirmed 170
Unknown 2
UserApprovalTimeout 1
PendingConfirmation 29
UserApprovalFailed 82
UserApprovalFailed 8
UserApprovalCancelled 14
UNKNOWN 1
UserApprovalFailed 16
Rejected 11
PendingConfirmation 76
PendingConfirmation 3
UserApprovalTimeout 6
Paid 2
Paid 1736
PendingUserApproval 790
UserApprovalTimeout 551
Confirmed 9
Paid 288
我希望行可以按STATUS列求和。我不想重建整个CASE函数。对此有什么解决方案吗?
2条答案
按热度按时间bqf10yzr1#
iq3niunx2#
只需将您的查询作为子查询,如下所示:
此致