DELETE FROM DC2.OR_100 WHERE PV_USER = '358';
commit;
INSERT INTO DC2.OR_100 (PV_KEY,PV_USER,PV1,PV2,PV3,PV4)
SELECT LPAD(TO_CHAR(ROWNUM),20,0),TABLE_SELECT.* FROM (
SELECT '358'
 ,SUBSTRB(CASE WHEN T007.NENGETSU=' ' OR T007.NENGETSU IS NULL THEN ' ' ELSE SUBSTR(T007.NENGETSU,1,4) || '/' || SUBSTR(T007.NENGETSU,5,2) END,1,100) AS A001
 ,SUBSTRB(RTRIM(TO_CHAR(T001.SOUKO)),1,100) AS A002
 ,SUBSTRB(RTRIM(TO_CHAR(MAX(T006.TOR_MEI))),1,100) AS A003
 ,SUBSTRB(TO_CHAR(SUM(T001.URI_KIN_ZEINUKI),'FM999999999999990'),1,100) AS A004
FROM
DC2_DEMO.SAM_DENPYO T001
LEFT OUTER JOIN DC2_DEMO.SAM_DAY T007 ON
 T001.DEN_DATE=T007.HIDUKE
LEFT OUTER JOIN DC2_DEMO.SAM_TORIHIKI T006 ON
 T001.SOUKO=T006.TOR_CD
 AND T006.TOR_KB='0'
WHERE
 T001.DEN_KB='2'
 AND T007.NENGETSU BETWEEN '20090101' AND '20091201'
GROUP BY
  T001.SOUKO
 ,T007.NENGETSU
ORDER BY
  T007.NENGETSU
) TABLE_SELECT
;
commit;
EXIT;


