如果where子句不匹配,则为列选择获取0

时间:2013-12-20 11:14:42

标签: sql jasper-reports

我在子报表中有以下SQL查询:

select ifnull(sum (commissionDocument.netto), 0) as commissionCreditNoteNetValue,
   ( select ifnull (sum (commissionDocument.netto ), 0)
     from tckopf as commissionDocument
     where commissionDocument.referenzid = $P{document_id} 
       and commissionDocument.btyp = 7) as commissionInvoiceNetValue
from tckopf as commissionDocument
where commissionDocument.referenzid = $P{document_id} 
  and commissionDocument.btyp = 8

问题是,如果where子句:

,我会得到一个空报告
where commissionDocument.referenzid = $P{document_id} 
  and commissionDocument.btyp = 8

不匹配,其他where子句匹配。

where commissionDocument.referenzid = $P{document_id} 
  and commissionDocument.btyp = 7
  1. SQL Fiddle example if the inner where clause match

  2. SQL Fiddle example if both where clauses match

  3. SQL Fiddle example if the outer where clause match

  4. 我忘了提及,FoxPro DBF包含数据,并通过JDBC驱动程序连接到 iReport

    子报表只有一个字段,其中包含以下表达式:

    ($F{commissionInvoiceNetValue} != null && $F{commissionCreditNoteNetValue} != null)
    ? ($F{commissionInvoiceNetValue} - $F{commissionCreditNoteNetValue})
    : ($F{commissionInvoiceNetValue} != null) ? $F{commissionInvoiceNetValue}
    : ($F{commissionCreditNoteNetValue} != null) ? $F{commissionCreditNoteNetValue}
    : ""
    

    我该如何绕过它?

2 个答案:

答案 0 :(得分:1)

changed the query来:

SELECT
(   SELECT IFNULL (SUM (commissionInvoice.netto ), 0) AS commissionInvoiceNetValue
    FROM tckopf AS commissionInvoice
    WHERE commissionInvoice.referenzid = 1
        AND commissionInvoice.btyp = 5 )
-
(   SELECT IFNULL (SUM (commissionCreditNote.netto), 0) AS commissionCreditNoteNetValue
    FROM tckopf AS commissionCreditNote
    WHERE commissionCreditNote.referenzid = 1
        AND commissionCreditNote.btyp = 6 )
 AS commissionResult

和以下表达式的字段:

$F{commissionResult}

现在我得到了理想的结果。

答案 1 :(得分:0)

我很瘦你可以通过外连接解决这个问题:

select ifnull(sum (cDoc.netto), 0) as cCNValue, CInvNoteVal.Val
from tckopf as cDoc
FULL OUTER JOIN
  (select ifnull (sum (cDoc.netto ), 0) AS Val, referenzid 
  from tckopf as cDoc
  where cDoc.referenzid = $P{document_id} and cDoc.btyp = 7) as CInvNoteVal
  ON CInvNoteVal.referenzid = cDoc.referenzid
  where cDoc.referenzid = $P{document_id} and cDoc.btyp = 8