使用联合查询计数不起作用

时间:2016-11-07 11:20:36

标签: plsql

我正在做一个循环,可以查看是否退出查询中的某个寄存器并使用union all。我做过这些:

DECLARE
COUNT INTEGER;
BEGIN
FOR I IN (SELECT CALENDAR FROM DF_CALENDAR) LOOP
    DBMS_OUTPUT.PUT_LINE(I.CALENDAR);
        SELECT COUNT(CONF_CAL) into CUENTA FROM (
                SELECT CONF_CAL from JOB j
                where CONF_CAL IS NOT NULL 
                UNION
                select DAYS_CAL from  JOB j
                where  DAYS_CAL  IS NOT NULL 
                UNION
                select weeks_cal from  JOB j
                where  weeks_cal  IS NOT NULL) WHERE CONF_CAL='I.CALENDAR';

                DBMS_OUTPUT.PUT_LINE(COUNT);
                DBMS_OUTPUT.PUT_LINE('CALENDAR ...'|| I.CALENDAR || ' COUNT NUMBER ' || COUNT);
END LOOP;
END;

所有计数变量显示0但是当我这样做时:

SELECT COUNT(CONF_CAL) FROM (
                SELECT CONF_CAL from JOB j
                where CONF_CAL IS NOT NULL 
                UNION
                select DAYS_CAL from  JOB j
                where  DAYS_CAL  IS NOT NULL 
                UNION
                select weeks_cal from  JOB j
                where  weeks_cal  IS NOT NULL) WHERE CONF_CAL='ALLDAYS'

它显示我的数量(conf_cal)= 1。

我无法理解为什么循环不起作用。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

这不应该对循环做任何事情。

我认为WHERE CONF_CAL='I.CALENDER'被解释为字符串而不是列值。

尝试删除'

WHERE CONF_CAL=I.CALENDER

答案 1 :(得分:0)

也许错字?

 COUNT(CONF_CAL) into CUENTA

应该是:

 COUNT(CONF_CAL) into COUNT

您正在声明并打印变量COUNT,而不是CUENTA

答案 2 :(得分:0)

为什么不简单:

SELECT dfc.calendar,
       COUNT(cc.conf_cal) count_cal
FROM   df_calendar dfc
       LEFT OUTER JOIN (SELECT conf_cal
                        FROM   job
                        WHERE  conf_cal IS NOT NULL
                        UNION
                        SELECT days_cal
                        FROM   job
                        WHERE  days_cal IS NOT NULL
                        UNION
                        SELECT weeks_cal
                        FROM   job
                        WHERE  weeks_cal IS NOT NULL) cc
         ON (dfc.calendar = cc.conf_cal)
GROUP BY dfc.calendar;