[SelectRecords] select ts.CD_CUSTOMER ,NAME_CUSTOMER || ' 御中' as NAME_CUSTOMER ,ts.DT_SEIKYU ,DTS_Y ,DTS_M ,DTS_D ,ts.MONEY_SEIKYU_KEY ,ts.RATE_TAX ,ts.TAX ,CD_PRODUCT ,NAME_PRODUCT ,CNT_DATA ,UNIT_PRICE_SALE ,ts.MONEY_SALE ,ts.DT_SEIKYU as NOW ,CASE WHEN FLG_FCS IS NULL THEN coalesce(NOTE,'') ELSE CASE WHEN FLG_FCS = 0 THEN coalesce(NOTE,'') ELSE coalesce(NOTE,'') || 'FSC Recycled 100%' END END NOTE ,FLG_FCS ,'COC認証番号 SA-COC-004068' FSC ,'' FSC2 ,ODR_DTS_Y ,ODR_DTS_M ,ODR_DTS_D ,tsh.MONEY_SEIKYU_KEY as MONEY_SEIKYU_KEY_HEADER ,to_char(tsh.RATE_TAX / 100.00, '0.00') as RATE_TAX_HEADER ,tsh.TAX as TAX_HEADER ,tsh.MONEY_SALE as MONEY_SALE_HEADER FROM( select ts.CD_CUSTOMER ,mc.NAME_CUSTOMER ,ts.DT_SEIKYU ,(to_char(ts.DT_SALE, 'yyyy')) ::numeric ::varchar DTS_Y ,(to_char(ts.DT_SALE, 'mm')) ::numeric ::varchar DTS_M ,(to_char(ts.DT_SALE, 'dd')) ::numeric ::varchar DTS_D ,ts.MONEY_SEIKYU_KEY ,to_char(ts.RATE_TAX / 100.00, '0.00') RATE_TAX ,ts.TAX ,ts.CD_PRODUCT ,CASE WHEN ts.DIV_SALE = 1 THEN '値引き' ELSE ts.NAME_PRODUCT END NAME_PRODUCT ,ts.CNT_DATA ,ts.UNIT_PRICE_SALE UNIT_PRICE_SALE ,ts.MONEY_SALE ,statement_timestamp() NOW ,ts.NOTE ,ts.FLG_FCS ,(to_char(ts.DT_SALE, 'yyyy')) ::numeric ODR_DTS_Y ,(to_char(ts.DT_SALE, 'mm')) ::numeric ODR_DTS_M ,(to_char(ts.DT_SALE, 'dd')) ::numeric ODR_DTS_D from T_SEIKYU_TBL ts left join M_CUSTOMER mc on ts.CD_CUSTOMER = mc.CD_CUSTOMER and mc.DT_START <= to_date('&1') and to_date('&1') <= mc.DT_END where ts.DT_SEIKYU = to_date('&1') and ts.FLG_DELETE = 0 and ts.CD_CUSTOMER <> 1 and mc.FLG_DELETE = 0 union all select t1.CD_CUSTOMER ,t1.NAME_CUSTOMER ,t1.DT_SEIKYU ,t1.DTS_Y ,t1.DTS_M ,t1.DTS_D ,(select sum(MONEY_SEIKYU_KEY) from ( select tst.CD_CUSTOMER,trunc(sum(tst.CNT_DATA) * tst.UNIT_PRICE_SALE) MONEY_SEIKYU_KEY from T_SEIKYU_TBL tst left join M_CUSTOMER m_c on tst.CD_CUSTOMER = m_c.CD_CUSTOMER where tst.DT_SEIKYU = to_date('&1') and tst.FLG_DELETE = 0 and tst.CD_CUSTOMER = 1 group by tst.CD_CUSTOMER, tst.UNIT_PRICE_SALE )alias22 ) MONEY_SEIKYU_KEY ,t1.RATE_TAX ,sum(t1.TAX) TAX ,t1.CD_PRODUCT ,t1. NAME_PRODUCT ,sum(t1.CNT_DATA) CNT_DATA ,t1. UNIT_PRICE_SALE ,trunc(sum(t1.CNT_DATA) * t1. UNIT_PRICE_SALE) MONEY_SALE ,t1.NOW ,t1.NOTE ,t1.FLG_FCS ,ODR_DTS_Y ,ODR_DTS_M ,ODR_DTS_D from ( select ts.CD_CUSTOMER ,mc.NAME_CUSTOMER ,ts.DT_SEIKYU ,ts.NO_LINE ,ts.DT_SALE ,(to_char(ts.DT_SALE, 'yyyy')) ::numeric ::varchar DTS_Y ,(to_char(ts.DT_SALE, 'mm')) ::numeric ::varchar DTS_M ,case when (substr(to_char(ts.DT_SALE, 'yyyymmdd'), 7, 2)) ::numeric < 21 then '1〜20' else '21〜' || to_char(((date_trunc('month', (ts.DT_SALE) ::timestamp + interval '1 month')) ::date - 1), 'dd') end DTS_D ,ts.MONEY_SEIKYU_KEY ,to_char(ts.RATE_TAX / 100.00, '0.00') RATE_TAX ,ts.TAX ,ts.CD_PRODUCT ,CASE WHEN ts.DIV_SALE = 1 THEN '値引き' ELSE ts.NAME_PRODUCT END NAME_PRODUCT ,ts.CNT_DATA ,CASE WHEN ts.MONEY_FRACTION_ADJUST IS NULL THEN ts.UNIT_PRICE_SALE WHEN ts.MONEY_FRACTION_ADJUST = 0 THEN ts.UNIT_PRICE_SALE ELSE null END UNIT_PRICE_SALE ,ts.MONEY_SALE ,statement_timestamp() NOW ,ts.NOTE ,ts.FLG_FCS ,(to_char(ts.DT_SALE, 'yyyy')) ::numeric ODR_DTS_Y ,(to_char(ts.DT_SALE, 'mm')) ::numeric ODR_DTS_M ,case when (substr(to_char(ts.DT_SALE, 'yyyymmdd'), 7, 2)) ::numeric < 21 then 1 else 2 end ODR_DTS_D from T_SEIKYU_TBL ts left join M_CUSTOMER mc on ts.CD_CUSTOMER = mc.CD_CUSTOMER and mc.DT_START <= to_date('&1') and to_date('&1') <= mc.DT_END where ts.DT_SEIKYU = to_date('&1') and ts.FLG_DELETE = 0 and ts.CD_CUSTOMER = 1 and mc.FLG_DELETE = 0 ) t1 group by t1.DTS_Y, t1.DTS_M, t1.DTS_D, t1.CD_PRODUCT, t1.NAME_PRODUCT, t1.CD_CUSTOMER, t1.NAME_CUSTOMER, t1.DT_SEIKYU, t1.RATE_TAX, t1.UNIT_PRICE_SALE, t1.NOW, t1.NOTE, t1.MONEY_SEIKYU_KEY, t1.FLG_FCS ,ODR_DTS_Y ,ODR_DTS_M ,ODR_DTS_D )ts left join T_SEIKYU_H tsh on ts.CD_CUSTOMER=tsh.CD_CUSTOMER and ts.DT_SEIKYU=tsh.DT_SEIKYU where ts.CD_CUSTOMER in (&2) order by ts.CD_CUSTOMER, NAME_CUSTOMER, ODR_DTS_Y, ODR_DTS_M, ODR_DTS_D, ts.DT_SEIKYU, CD_PRODUCT, NAME_PRODUCT, ts.RATE_TAX