我有一种方法可以重写 SQL 查询,所以我可以避免IFNULL
or COALESCE
,因为 HXTT DBF JDBC 驱动程序(支持 SQL92)会抛出以下错误:
Caused by: java.sql.SQLException: The aggregate function SUM(NETTO) can only be used in select-list, HAVING clause, and ORDER BY clause of SELECT statement.
以下查询导致上述错误:
SELECT
( SELECT IFNULL (SUM (shipManagementInvoice.netto ), 0) AS shipManagementInvoiceNetValue
FROM tckopf AS shipManagementInvoice
WHERE shipManagementInvoice.referenzid = 1
AND shipManagementInvoice.btyp = 5 )
-
( SELECT IFNULL (SUM (shipManagementCreditNote.netto), 0) AS shipManagementCreditNoteNetValue
FROM tckopf AS shipManagementCreditNote
WHERE shipManagementCreditNote.referenzid = 1
AND shipManagementCreditNote.btyp = 6 )
AS shipManagementResult
如果我删除IFNULL
or COALESCE
,则没有错误。
有几种方法可以重写查询,甚至更多,因为两个子查询都使用相同的基表。不知道为什么会抛出错误以及应该归咎于谁,JDBC驱动程序,Foxpro或其他东西,所以这里有一些替代方案:
(1) 对两个搜索使用一个查询:
(2) 在外部级别中拉取
COALESCE()
orIFNULL()
函数调用,或者使用原始查询:(2b) 或上述 (1st) 查询:
这也有效。:
尝试
ISNULL(SUM (shipManagementInvoice.netto ), 0)