从其他 table 中选择日期的列的总和在比较日期时给我错误的答案

sum of column with dates selected from other table against dates give me wrong answer when compare dates

我写了一个代码,它给出了错误的答案它做了什么它比较两个 table 的日期并检查它的星期日和 return 我它检查的 item 列的总和星期天它工作正常但是当它检查其他 table 日期并与它自己的日期进行比较然后给出总和是错误的请帮助解决这个问题谢谢 您还可以在此处查看 dbfiddle

DECLARE
a number;
B DATE;
CURSOR GZDT IS
SELECT GAZZETED_DATE  FROM GAZZETED_DAYS
WHERE GAZZETED_DATE between date '2021-01-01' and date '2021-01-20';

begin
    OPEN GZDT;
    LOOP 
        FETCH GZDT INTO B;      
         EXIT WHEN GZDT%notfound;
    END LOOP;
    CLOSE GZDT;
SELECT  SUM(ITEM) into a from pay_in_out
  where TO_CHAR(att_date,'DY', 'NLS_DATE_LANGUAGE=AMERICAN') NOT IN ('SUN')
   AND ATT_DATE != B
  AND att_date between date '2021-01-01' and date '2021-01-20'  
  AND EMP_CODE = 100;
  dbms_output.put_line('the sum of item is :'||a) ;
  
end;
/

从您的 dbfiddle 中的示例数据来看,您似乎想要计算未出现在 GAZZETED_DAYS table.

中的所有日期的值

不幸的是,您的 B 变量只包含一个 date 值,而您的 table 有 5 个示例值。结果,您的游标 LOOP 覆盖了 B 5 次,最后只有 26-JAN-21.

如果您想对照 all 检查 table 中的值,NOT IN 子句可能是比游标和变量更简单的解决方案.

DECLARE
  a number;

begin

SELECT  SUM(ITEM) into a from pay_in_out
  where 
   ATT_DATE NOT IN 
     (SELECT GAZZETED_DATE FROM GAZZETED_DAYS
      WHERE GAZZETED_DATE between date '2021-01-01' and date '2021-01-20')
  AND att_date between date '2021-01-01' and date '2021-01-20'  
  AND EMP_CODE = 100;
  dbms_output.put_line('the sum of item is :'||a) ;
  
end;
/