我正在准备一个报告,其中需要添加两个case语句的结果并将其显示在新列中。
我尝试这样做:SUM('4MPBS')+ SUM('8MBPS')AS总计 但没有给出正确的数字。SELECT DATE(bc.cycle_start_date) AS cycle,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 2MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 2MBPS,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 4MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 4MBPS,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 8MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 8MBPS,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 16MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 16MBPS,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 25MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 25MBPS,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 50MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 50MBPS,
SUM( CASE WHEN bib.`category`='Discount Monthly Charges' THEN (bib.`itemDisplayValue`*-1) ELSE 0 END) AS discount,
SUM('4MPBS') +SUM('8MBPS') AS total
FROM `broadband_invoicebreakdown` bib,`broadband_invoicedetails` bid,`broadbandcycle` bc
WHERE bib.`invoiceId`=bid.invoiceid AND bid.cycleid=bc.cycle_id AND bc.cycle_type !='U'
GROUP BY bc.cycle_type
我得到的结果如下。第3列和第4列的总值有误。
周期2MBPS 4MBPS 8MBPS 16MBPS 25MBPS 50MBPS总共折扣
2019-04-01 0.00 116.88 260.00 272.00 940.64 0.00 0.00 2472 2019-03-15 0.00 95295.20 34740.00 22644.00 8268.96 4050.00 0.00 44544 2019-03-22 0.00 41745.00 15710.00 8704.00 2668.76 3712.50 0.00 21996
它应该像这样:
周期2MBPS 4MBPS 8MBPS 16MBPS 25MBPS 50MBPS总共折扣
2019-04-01 0.00 116.88 260.00 272.00 940.64 0.00 0.00 376.88 2019-03-15 0.00 95295.20 34740.00 22644.00 8268.96 4050.00 0.00 13003.00 2019-03-22 0.00 41745.00 15710.00 8704.00 2668.76 3712.50 0.00 57455.00
答案 0 :(得分:0)
你可以试试吗?
SELECT DATE(bc.cycle_start_date) AS cycle,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 2MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 2MBPS,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 4MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 4MBPS,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 8MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 8MBPS,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 16MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 16MBPS,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 25MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 25MBPS,
SUM(CASE WHEN bib.`broadbandPlan`='Smart Broadband - 50MB' THEN bib.`itemDisplayValue` ELSE 0 END) AS 50MBPS,
SUM( CASE WHEN bib.`category`='Discount Monthly Charges' THEN (bib.`itemDisplayValue`*-1) ELSE 0 END) AS discount,
SUM(CASE WHEN bib.broadbandPlan='Smart Broadband - 4MB' THEN bib.itemDisplayValue ELSE 0 END) + SUM(CASE WHEN bib.broadbandPlan='Smart Broadband - 8MB' THEN bib.itemDisplayValue ELSE 0 END) AS total
FROM `broadband_invoicebreakdown` bib,`broadband_invoicedetails` bid,`broadbandcycle` bc
WHERE bib.`invoiceId`=bid.invoiceid AND bid.cycleid=bc.cycle_id AND bc.cycle_type !='U'
GROUP BY bc.cycle_type