我已经更新了这个问题,因为我已经取得了进展,我的HAVING
陈述仍然无法正常工作。我得到一个ORA-00933: SQL command not properly ended
..
我相信这是我的HAVING
条款。有任何想法吗?
回顾:
我正在尝试捕获 2013-2015 年间没有购买活动的客户的进度,并从 2015 年 10 月开始跟踪他们的购买活动,以跟踪销售人员的活动。
SELECT INV.COMPANY
, INV.CUSTOMER_ID
, INV.ADDRESS_ID
, INV.NAME
, INV.ADDRESS1
, INV.ADDRESS2
, INV.CITY
, INV.STATE
, INV.ZIP_CODE
, INV.COUNTRY
, CICM.CLIENT_VALUE as contact_type
, CICM.VALUE
, CICM.DESCRIPTION
, INV.REGION_CODE
, INV.SITE
, LD.LSTINVDATE
, CASE WHEN TO_DATE(INV.INVDATE) BETWEEN TO_DATE('01-Oct-13') AND TO_DATE('30-Sep-15') THEN SUM(INV.SALES) ELSE 0 END AS TWO_YEARS
, CASE WHEN TO_DATE(INV.INVDATE) BETWEEN TO_DATE('01-Oct-15') AND SYSDATE THEN SUM(INV.SALES) ELSE 0 END AS PROGRESS
, SUM(INV.SALES) AS SLSAMNT
FROM IFSINFO.HB_INVOICING_ALL INV
LEFT JOIN IFSINFO.HB_LAST_INVOICE_DATE LD
ON (INV.COMPANY = LD.COMPANY)
AND (INV.CUSTOMER_ID = LD.IDENTITY)
LEFT JOIN IFSAPP.CUSTOMER_INFO_COMM_METHOD CICM
ON (INV.ADDRESS_ID = CICM.ADDRESS_ID)
AND (INV.CUSTOMER_ID = CICM.CUSTOMER_ID)
WHERE INV.CUST_GRP = '4'
AND INV.COMPANY = '20'
HAVING SUM(INV.INVDATE) BETWEEN TO_DATE('01-Oct-13') AND TO_DATE('30-Sep-15') = 0
--HAVING MAX(LD.LSTINVDATE) < (SYSDATE - INTERVAL '2' YEAR)
GROUP BY INV.COMPANY
, INV.CUSTOMER_ID
, INV.ADDRESS_ID
, INV.NAME
, INV.ADDRESS1
, INV.ADDRESS2
, INV.CITY
, INV.STATE
, INV.ZIP_CODE
, INV.COUNTRY
, CICM.CLIENT_VALUE
, CICM.VALUE
, CICM.DESCRIPTION
, INV.REGION_CODE
, INV.SITE
, LD.LSTINVDATE
, INV.INVDATE
该
HAVING
子句是错误的原因,因为它在语法上不正确。你有(添加括号以显示它是如何解析的):括号内的表达式返回布尔结果。
此外,
SUM(INV.INVDATE)
没有多大意义。如果我们添加日期,人们会期待什么结果?正如@mustaccio 正确注意到的那样,该
HAVING
子句在之后GROUP BY
,而不是之前。看起来你想要在
HAVING
子句中这样的东西:我更愿意改写为:
the
INV.INVDATE
也应该从GROUP BY
子句中删除,TWO_YEARS
andPROGRESS
表达式也应该更正为SUM(...)
建议的表达式SUM(CASE WHEN ... THEN x ELSE y END)
,而不是CASE WHEN ... THEN SUM(x) ELSE y END
.