我使用以下查询返回账单的总额,税金和总和税金的总额。但问题是,因为我使用sum()它只返回第一个如何检索所有行和总计?
SELECT sum(
at_salesbill.`billedTotal` + at_salesbill.`billedTotalTax`
) as grantTotal,
at_salesbill.`billedTotal` AS at_salesbill_billedTotal,
at_salesbill.`billedTotalTax` AS billedTotalTax,
FROM at_salesbill
WHERE at_salesbill.`billGuid` = 44;
答案 0 :(得分:1)
您只需添加值即可获得总和:
SELECT (at_salesbill.`billedTotal`+at_salesbill.`billedTotalTax`) as grantTotal,
at_salesbill.`billedTotal` AS at_salesbill_billedTotal,
at_salesbill.`billedTotalTax` AS billedTotalTax,
from at_salesbill
where at_salesbill.`billGuid` = 44;
然后,假设行中有唯一的id
,您可以通过一个技巧来获取总数和值:
SELECT at_salesbill.id,
sum(at_salesbill.`billedTotal`+at_salesbill.`billedTotalTax`) as grantTotal,
at_salesbill.`billedTotal` AS at_salesbill_billedTotal,
at_salesbill.`billedTotalTax` AS billedTotalTax,
from at_salesbill
where at_salesbill.`billGuid` = 44
group by at_salesbill.id with rollup;
在这里,sum()
没有任何意义 - 因为每组中都有一行。但是,with rollup
会添加一个总列,其中id
为NULL
。
SELECT (case when at_salesbill.id is NULL then 'total' else '' end),
sum(at_salesbill.`billedTotal`+at_salesbill.`billedTotalTax`) as grantTotal,
at_salesbill.`billedTotal` AS at_salesbill_billedTotal,
at_salesbill.`billedTotalTax` AS billedTotalTax,
from at_salesbill
where at_salesbill.`billGuid` = 44
group by at_salesbill.id with rollup;