2017-08-14 105 views
1

我已經創建了2個case語句來從單個表中提取數據(它被創建爲2個單獨的case語句,因爲我想對double值進行計數,並且作爲單個case語句,它沒有做這個)。如何結合兩個case case列

這兩個case語句導致了2個新的數據列(case case A和case statement 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

你的情況下表達列都需要不同的別名。 – jarlh

+0

好的,我可以做到這一點,但我該如何將它們結合在一起? – mrochester

+2

添加一些示例表格數據和預期結果 - 作爲格式化文本! – jarlh

回答

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; 
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

0

謝謝你的幫助。 Gordon Linoff,我已經嘗試過您的查詢,但是這會得到我在將案例陳述分解爲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付款。

這就是我的表最終如何看待單個案例的聲明。請注意,沒有Internet付款或付款條目。

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 
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;