我已经创建了2个case语句来从单个表中提取数据(它被创建为2个单独的case语句,因为我想要重复计算值,并且作为单个case语句,它没有这样做)。
2个案例陈述产生了2个新的数据列(案例陈述A和案例陈述B)。但是,我希望在一个列中报告2个case语句值,所以我试图找到一种方法将这两个列组合在一起。
这是我的查询。我对SQL很新(只用了一个星期!)所以请原谅我查询中的任何愚蠢的语法。
select b.part_month, count(a.pay_code), sum(a.amount),
CASE
when a.pay_code like 'CC%' THEN 'Credit Card Payment'
when a.pay_code like 'DC%' then 'Debit Card Payment'
Else 'N/A'
End as Payment_Type,
CASE
when a.pay_code like '%IP%' then 'Internet Payment'
when a.pay_code like '%TT%' then 'Touchtone Payment'
Else 'N/A'
End as Payment_Type
from table_a A
inner join table_b B on A.form_ID = B.ID
where a.trans_type > 0
and b.st_id = 2
group by B.PART_MONTH,
CASE
when a.pay_code like 'CC%' THEN 'Credit Card Payment'
when a.pay_code like 'DC%' then 'Debit Card Payment'
Else 'N/A'
End,
CASE
when a.pay_code like '%IP%' then 'Internet Payment'
when a.pay_code like '%TT%' then 'Touchtone Payment'
Else 'N/A'
End
order by b.part_month asc;
答案 0 :(得分:1)
为您提供CASE
个别名,然后将查询包装到GROUP BY
个别名。
结合您的CASE
语句,假设这是您想要的。
SELECT * FROM (
SELECT b.part_month,
COUNT(a.pay_code) AS PayCodeCount,
SUM(a.amount) AS AmountSum,
CASE a.pay_code
WHEN 'CC%' THEN 'Credit Card Payment'
WHEN 'DC%' THEN 'Debit Card Payment'
WHEN '%IP%' THEN 'Internet Payment'
WHEN '%TT%' THEN 'Touchtone Payment'
ELSE 'N/A'
END AS Payment_Type
FROM table_a A
INNER JOIN table_b B ON A.form_ID = B.ID
WHERE a.trans_type > 0
AND b.st_id = 2) Z
GROUP BY PART_MONTH, Payment_Type
ORDER BY part_month ASC;
答案 1 :(得分:0)
我猜你想要:
select b.part_month, count(a.pay_code), sum(a.amount),
(case when a.pay_code like 'CC%' then 'Credit Card Payment'
when a.pay_code like 'DC%' then 'Debit Card Payment'
when a.pay_code like '%IP%' then 'Internet Payment'
when a.pay_code like '%TT%' then 'Touchtone Payment'
else 'N/A'
end) as Payment_Type
from table_a a inner join
table_b b
on a.form_ID = b.ID
where a.trans_type > 0 and b.st_id = 2
group by b.part_month,
(case when a.pay_code like 'CC%' then 'Credit Card Payment'
when a.pay_code like 'DC%' then 'Debit Card Payment'
when a.pay_code like '%IP%' then 'Internet Payment'
when a.pay_code like '%TT%' then 'Touchtone Payment'
else 'N/A'
end)
order by b.part_month asc;
重要的是:某些数据库允许您使用group by
中的别名,接受group by b.part_month, payment_type
。
答案 2 :(得分:0)
感谢您的帮助。 Gordon Linoff,我已经尝试了你的查询,但是这让我得到了我最初开始的结果,然后将case语句拆分为2。
pay_code%IP%和%TT%也可以匹配CC%和DC%语句,如果匹配,则不计算IP或TT值。但是,我需要计算匹配%IP%,%TT%,CC%和DC%的所有值,即使这意味着重复计算某些数据行。
因此,pay_code列中的值可能是“DCIP”,“CCTT”,“CCIP”等。从这些值中,我想知道有1个DC付款,2个CC付款,2个IP付款和1个TT付款。
这就是我的表最终会看到单个case语句的方式。请注意,没有互联网付款或按键付款条目。
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
N/A
Credit Card Payment
Debit Card Payment
答案 3 :(得分:0)
答案是
Select * from (select b.part_month, count(a.pay_code), sum(a.amount),
CASE
when a.pay_code like '%IP%' then 'Internet Payment'
when a.pay_code like '%TT%' then 'Touchtone Payment'
Else 'N/A'
End as Payment_Type
from table_a A
inner join table_b B on A.form_ID = B.ID
where a.trans_type > 0
and b.st_id = 2
group by B.PART_MONTH,
CASE
when a.pay_code like '%IP%' then 'Internet Payment'
when a.pay_code like '%TT%' then 'Touchtone Payment'
Else 'N/A'
End
Union
select b.part_month, count(a.pay_code), sum(a.amount),
CASE
when a.pay_code like 'CC%' THEN 'Credit Card Payment'
when a.pay_code like 'DC%' then 'Debit Card Payment'
Else 'N/A'
End as Payment_Type
from table_a A
inner join table_b B on A.form_ID = B.ID
where a.trans_type > 0
and b.st_id = 2
group by B.PART_MONTH,
CASE
when a.pay_code like 'CC%' THEN 'Credit Card Payment'
when a.pay_code like 'DC%' then 'Debit Card Payment'
Else 'N/A'
End)
Order by 1,4;