我有以下Mysql程序。
select
case temp.lineItemTypeID when 1 then temp.tt else 0 end case as planTotalAmount,
case temp.lineItemTypeID when 2 then temp.tt else 0 end case as discountTotalAmount
from (
SELECT
lineItemTypeID,
sum(lineitemAmount) as tt from tbl_invoicelineitem
where date(lineItemcreatedDate) IS NOT NULL
group by lineItemTypeID
) temp;
我希望得到单个的结果,即子查询返回。 例如:
planTotalAmount | discountTotalAmount
10 | 2 `
但我正在
planTotalAmount | discountTotalAmount
10 | 0
0 | 2 `
如何解决这个问题?
答案 0 :(得分:1)
汇总case...end
表达式:
select
sum(case temp.lineItemTypeID when 1 then temp.tt else 0 end case) as planTotalAmount,
sum(case temp.lineItemTypeID when 2 then temp.tt else 0 end case) as discountTotalAmount
from (
SELECT
lineItemTypeID,
sum(lineitemAmount) as tt
from tbl_invoicelineitem
where date(lineItemcreatedDate) IS NOT NULL
group by lineItemTypeID
) temp;