I want to tweak the following SQL so the query will output the type of card (visa, mastercard, etc) as PaymentMethod instead of Credit Card.
CASE WHEN pm.PaymentType = 1 THEN 'Cash'
WHEN pm.PaymentType = 2 THEN 'Check'
WHEN pm.PaymentType = 3 THEN 'Credit Card'
WHEN pm.PaymentType = 4 THEN 'EFT'
WHEN pm.PaymentType = 5 THEN 'Money Order'
WHEN pm.PaymentType = 6 THEN 'Conveyance'
ELSE 'Unknown'
END AS PaymentMethod,
Can someone please point me in the right direction. I tried adding a second conditional to my case statement, but it fails out to 'Unknown' for all payment types of 3.
CASE WHEN pm.PaymentType = 1 THEN 'Cash'
WHEN pm.PaymentType = 2 THEN 'Check'
WHEN pm.PaymentType = 3 and pm.CardTypeMId = 1 THEN 'American Express'
WHEN pm.PaymentType = 3 and pm.CardTypeMId = 2 THEN 'Discover'
WHEN pm.PaymentType = 3 and pm.CardTypeMId = 3 THEN 'Mastercard'
WHEN pm.PaymentType = 3 and pm.CardTypeMId = 4 THEN 'Visa'
WHEN pm.PaymentType = 4 THEN 'EFT'
WHEN pm.PaymentType = 5 THEN 'Money Order'
WHEN pm.PaymentType = 6 THEN 'Conveyance'
ELSE 'Unknown'
END AS PaymentMethod,
Thank you in advance for your assistance.
You could simplify this quite a bit.
CASE pm.PaymentType
WHEN 1 THEN 'Cash'
WHEN 2 THEN 'Check'
WHEN 3 THEN
CASE pm.CardTypeMId
WHEN 1 THEN 'American Express'
WHEN 2 THEN 'Discover'
WHEN 3 THEN 'Mastercard'
WHEN 4 THEN 'Visa'
END
WHEN 4 THEN 'EFT'
WHEN 5 THEN 'Money Order'
WHEN 6 THEN 'Conveyance'
ELSE 'Unknown'
END AS PaymentMethod
You can use nested CASE
WHEN pm.PaymentType = 3 THEN
Case
WHEN pm.CardTypeMId = 1 THEN 'American Express'
WHEN pm.CardTypeMId = 2 THEN 'Discover'
WHEN pm.CardTypeMId = 3 THEN 'Mastercard'
WHEN pm.CardTypeMId = 4 THEN 'Visa'
END as CreditCard
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With