SUM(item_name),不应计算空行

时间:2013-07-01 11:37:33

标签: java mysql sql

我有二十行,我正在使用sum()

SELECT Sum(totalcon) AS totalcon_sum,
   Sum(vat3)     AS vat3_sum,
   Sum(rupes)    AS rupes_sum,
   pordernon,
   po_date,
   b_no,
   vendortext
FROM   po,
   potable,
   branch_master,
   delivery_master
WHERE  mr_assetpo = 'NCE'
   AND Substr(po.pordernon, 6, 2) = branch_master.b_no
   AND po.pordernon = delivery_master.deliverypo_no
   AND po.transid = potable.transid
   AND Year(po_date) = '"+req2+"'
   AND status = 'normal'
GROUP  BY Year(po_date)  

我的问题是:

每当我之间有空行,总和也在计算那一行。我不想计算空行。

2 个答案:

答案 0 :(得分:0)

你可以添加这个

   WHERE totalcon != ''

或者如果它的null使用此

   WHERE totalcon is not null

答案 1 :(得分:0)

使用IFNULL功能

Sum(IFNULL(totalcon,0))

您的查询应该是: -

SELECT Sum(IFNULL(totalcon)) AS totalcon_sum,
   Sum(IFNULL(vat3))     AS vat3_sum,
   Sum(IFNULL(rupes))    AS rupes_sum,
   pordernon,
   po_date,
   b_no,
   vendortext
FROM   po,
   potable,
   branch_master,
   delivery_master
WHERE  mr_assetpo = 'NCE'
   AND Substr(po.pordernon, 6, 2) = branch_master.b_no
   AND po.pordernon = delivery_master.deliverypo_no
   AND po.transid = potable.transid
   AND Year(po_date) = '"+req2+"'
   AND status = 'normal'
GROUP  BY Year(po_date)