CASE声明发布日期

时间:2016-03-29 10:13:03

标签: mysql case case-statement

这是我的表结构。

===============================================
main_section | date_of_pay | currency | amount
===============================================

Tender         20-02-2016    INR        2000
Tender         20-02-2016    INR        1500
Tender         20-02-2016    INR        1850
PBG            20-02-2016    INR        1850
SD             20-02-2016    INR        1850
===============================================

我试图做一个CASE声明,如果'main_section = Tender'它应该返回投标金额,currency和date_of_pay。如果它的PBG它应该返回PBG金额,货币和date_of_pay。同时为SD。如果有多个记录,那么它应该总和并返回金额(此处招标有多个记录)。谁能帮我这个。以下是我试过的案例陈述。它工作正常,但没有显示date_of_pay,它显示为空(NULL)。

SELECT

SUM(CASE WHEN main_section = 'Tender' THEN com_payments.amount END) AS tendamount,
SUM(CASE WHEN main_section = 'SD' THEN com_payments.amount END) AS banksdamount,  
SUM(CASE WHEN main_section = 'PBG' THEN com_payments.amount END) AS bankpbgamount,     

MAX(CASE WHEN main_section = 'Tender Fee' THEN com_payments.amt_curr END) AS tendcurrency,
MAX(CASE WHEN main_section = 'Bank Guarantee SD' THEN com_payments.amt_curr END) AS banksdcurrency,
MAX(CASE WHEN main_section = 'Bank Guarantee PBG' THEN com_payments.amt_curr END) AS bankpbgcurrency,

CASE WHEN main_section = 'Tender Fee' THEN com_payments.date_of_pay END AS tendbankdop,
CASE WHEN main_section = 'Bank Guarantee SD' THEN com_payments.date_of_pay END AS banksdbankdop,
CASE WHEN main_section = 'Bank Guarantee PBG' THEN com_payments.date_of_pay END AS bankpbgbankdop

FROM TABLE

可能是我犯了一个错误。不确定。有人帮我吗?

0 个答案:

没有答案