today:
2,064
yesterday:
2,073
Total:
1,301,967

SQL Monthly SUM

admin 2023.05.22 20:03 Views : 161

DECLARE

 YEAR NUMBER;

 

BEGIN

DELETE FROM HISTORY_SUM;

INSERT INTO HISTORY_SUM (YEAR)

Select DISTINCT(TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY'))) as YEAR from HISTORY

ORDER BY YEAR;

 

     FOR x IN (Select * from HISTORY_SUM) LOOP

     

     UPDATE HISTORY_SUM

     SET JAN = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 1)

     WHERE YEAR = x.YEAR;

     

     UPDATE HISTORY_SUM

     SET FEB = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 2)

     WHERE YEAR = x.YEAR;

     

     UPDATE HISTORY_SUM

     SET MAR = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 3)

     WHERE YEAR = x.YEAR;

     

     UPDATE HISTORY_SUM

     SET APR = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 4)

     WHERE YEAR = x.YEAR;     

     

     UPDATE HISTORY_SUM

     SET MAY = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 5)

     WHERE YEAR = x.YEAR;

     

     UPDATE HISTORY_SUM

     SET JUN = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 6)

     WHERE YEAR = x.YEAR;

     

     UPDATE HISTORY_SUM

     SET JUL = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 7)

     WHERE YEAR = x.YEAR;

     

     UPDATE HISTORY_SUM

     SET AUG = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 8)

     WHERE YEAR = x.YEAR;

     

     UPDATE HISTORY_SUM

     SET SEP = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 9)

     WHERE YEAR = x.YEAR;

     

     UPDATE HISTORY_SUM

     SET OCT = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 10)

     WHERE YEAR = x.YEAR;     

     

     UPDATE HISTORY_SUM

     SET NOV = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 11)

     WHERE YEAR = x.YEAR;

     

     UPDATE HISTORY_SUM

     SET DEC = (Select SUM(TOTAL_$) from HISTORY c

WHERE TO_NUMBER(TO_CHAR(VISIT_DATE, 'YYYY')) = YEAR AND TO_NUMBER(TO_CHAR(VISIT_DATE, 'MM')) = 12)

     WHERE YEAR = x.YEAR;

        

  END LOOP;    

 

COMMIT;

END;

No. Subject Author Date Views
129 127.0.0.1 issues admin 2024.09.09 228
128 GP 영수증 FOOTER admin 2024.06.30 366
127 GP 영수증 HEADER admin 2024.06.30 369
126 LAST DAY SQL admin 2024.01.19 341
125 Null Display Value admin 2024.01.10 327
124 영수증 2023 file admin 2024.01.04 457
123 Column Data 일괄 추가 admin 2023.11.24 294
122 Using SQL*Plus to Unlock and Change Passwords admin 2023.06.04 288
121 Reversing Require HTTPS admin 2023.06.04 177
120 ipconfig admin 2023.06.03 156
119 127.0.0.1:8080/apex/ admin 2023.06.03 190
118 CHANGE TABLE NAME admin 2023.05.31 205
117 DATE -> DAY CONVERT SQL admin 2023.05.25 123
116 YEAR EXPRESSIONS admin 2023.05.25 124
115 GRAPH (BAR) SQL SAMPLE admin 2023.05.25 133
» Monthly SUM admin 2023.05.22 161
113 INNER JOIN admin 2023.05.19 288
112 LOOP TABLE UPDATE admin 2023.05.19 339
111 Global pages admin 2023.05.09 120
110 ORACLE COLLECTION admin 2023.05.08 134