我正在尝试构建一个查询,它返回某个值的每个月的计数(传递开始日期和结束日期),输出应该是这样的
Month Qtn
---------
|July|0|
|Augu|0|
|Sept|0|
但我没有行,查询如下:
SELECT TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM') AS XMONTH, COUNT(TT_CUSTOMERS.NAMES) AS QTY FROM TT_CUSTOMERS
WHERE COMPANY = 700
AND TT_CUSTOMERS.C1_LOOKUP_ID = 100
AND TT_CUSTOMERS.C2_LOOKUP_ID = 1
AND TT_CUSTOMERS.C3_LOOKUP_ID IN (70, 80)
AND TT_CUSTOMERS.ST_LOOKUP_ID = 90
AND TO_NUMBER(TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMMDD')) >= TO_NUMBER('20170701')
AND TO_NUMBER(TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMMDD')) <= TO_NUMBER('20170930')
GROUP BY TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM')
ORDER BY TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM') ASC;
我知道这是因为查询没有返回任何行,所以XMONTH是emtpy,所以qty也是空的,但我无法找到一种方法来获得我已经显示的输出。
答案 0 :(得分:1)
假设closed_date是日期数据类型...... 并假设所有其他字段值都是int并且数据库中存在匹配...
我假设它是日期值的数字比较。比较日期作为日期而不是数字。
SELECT TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM') AS XMONTH
, COUNT(TT_CUSTOMERS.NAMES) AS QTY
FROM TT_CUSTOMERS
WHERE COMPANY = 700
AND TT_CUSTOMERS.C1_LOOKUP_ID = 100
AND TT_CUSTOMERS.C2_LOOKUP_ID = 1
AND TT_CUSTOMERS.C3_LOOKUP_ID IN (70, 80)
AND TT_CUSTOMERS.ST_LOOKUP_ID = 90
AND TT_CUSTOMERS.CLOSED_DATE >= TO_DATE('20170701','YYYYMMDD')
AND TT_CUSTOMERS.CLOSED_DATE <= TO_DATE('20170930','YYYYMMDD')
GROUP BY TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM')
ORDER BY TO_CHAR(TT_CUSTOMERS.CLOSED_DATE, 'YYYYMM') ASC;