[SelectRecords]
select
	mc.DTS_YM
--#24441 2022.06.18 MOD STT
--	,to_date(mc.DTS_YM || '01','yyyy/mm/dd') TITLE
	,to_date(mc.DTS_YM || '01','yyyymmdd') TITLE
--#24441 2022.06.18 MOD END
	,CASE WHEN ts.CD_CUSTOMER IS NULL THEN mc.CD_CUSTOMER  ELSE ts.CD_CUSTOMER END  CD_CUSTOMER
	,mc.NAME_CUSTOMER
	,CASE WHEN ts.WEIGHT_SALE IS NULL THEN 0  ELSE ts.WEIGHT_SALE END  WEIGHT_SALE
FROM (
	select
		DTS_YEAR || lpad(DTS_MONTH,2,'0') DTS_YM
		,mc.CD_CUSTOMER
		,sum(WEIGHT_SALE) WEIGHT_SALE
		,mc.DT_START
		,mc.DT_END
	from T_SALE
	left join M_CUSTOMER mc on T_SALE.CD_CUSTOMER = mc.CD_CUSTOMER and mc.DT_START <= T_SALE.DT_SALE and T_SALE.DT_SALE <= mc.DT_END
--#24441 2022.06.18 MOD STT
--	where T_SALE.DT_SALE >= to_date('&1'||'01','yyyy/mm/dd')
--	and T_SALE.DT_SALE <= ((date_trunc('month',(to_date('&2'||'01','yyyy/mm/dd'))::timestamp + interval '1 month'))::date - 1)
	where T_SALE.DT_SALE >= to_date('&1'||'01','yyyymmdd')
	and T_SALE.DT_SALE <= ((date_trunc('month',(to_date('&2'||'01','yyyymmdd'))::timestamp + interval '1 month'))::date - 1)
--#24441 2022.06.18 MOD END
	and T_SALE.FLG_DELETE = 0
--#24441 2022.06.18 MOD STT
--	and (to_date('&1'||'01','yyyy/mm/dd') < mc.DT_END and mc.DT_START <= to_date('&2'||'01','yyyy/mm/dd'))
	and (to_date('&1'||'01','yyyymmdd') < mc.DT_END and mc.DT_START <= to_date('&2'||'01','yyyymmdd'))
--#24441 2022.06.18 MOD END
	group by DTS_YEAR, DTS_MONTH, mc.CD_CUSTOMER, mc.DT_START, mc.DT_END
	order by DTS_YEAR, DTS_MONTH, mc.CD_CUSTOMER, mc.DT_START, mc.DT_END
) ts
right join(
	select
		tb.DTS_YM
		,tb.CD_CUSTOMER
		,tb.NAME_CUSTOMER
		,tb.DT_START
		,tb.DT_END
	from (
		select
			cu.CD_CUSTOMER
			,cu.NAME_CUSTOMER
			,substr(to_char(ca.CALENDAR,'yyyymmdd'),1,6) DTS_YM
			,cu.DT_START
			,cu.DT_END
		from M_CUSTOMER cu, M_CALENDAR ca
--#24441 2022.06.18 MOD STT
--		where ca.CALENDAR >= to_date('&1'||'01','yyyy/mm/dd')
--		and ca.CALENDAR <= ((date_trunc('month',(to_date('&2'||'01','yyyy/mm/dd'))::timestamp + interval '1 month'))::date - 1)
		where ca.CALENDAR >= to_date('&1'||'01','yyyymmdd')
		and ca.CALENDAR <= ((date_trunc('month',(to_date('&2'||'01','yyyymmdd'))::timestamp + interval '1 month'))::date - 1)
--#24441 2022.06.18 MOD END
		and cu.FLG_DELETE = 0
--#24441 2022.06.18 MOD STT
--		and (to_date('&1'||'01','yyyy/mm/dd') < cu.DT_END and cu.DT_START <= to_date('&2'||'01','yyyy/mm/dd'))
		and (to_date('&1'||'01','yyyymmdd') < cu.DT_END and cu.DT_START <= to_date('&2'||'01','yyyymmdd'))
--#24441 2022.06.18 MOD END
	) tb
	group by tb.DTS_YM, tb.CD_CUSTOMER, tb.NAME_CUSTOMER, tb.DT_START, tb.DT_END
	order by tb.DTS_YM, tb.CD_CUSTOMER, tb.NAME_CUSTOMER, tb.DT_START, tb.DT_END
--#24441 2022.06.18 MOD STT
--) mc on ts.CD_CUSTOMER = mc.CD_CUSTOMER and ts.DTS_YM = mc.DTS_YM and (ts.DT_START = mc.DT_START or ts.DT_END = mc.DT_END) and(to_date('&1'||'01','yyyy/mm/dd') < mc.DT_END and mc.DT_START <= to_date('&2'||'01','yyyy/mm/dd'))
) mc on ts.CD_CUSTOMER = mc.CD_CUSTOMER and ts.DTS_YM = mc.DTS_YM and (ts.DT_START = mc.DT_START or ts.DT_END = mc.DT_END) and(to_date('&1'||'01','yyyymmdd') < mc.DT_END and mc.DT_START <= to_date('&2'||'01','yyyymmdd'))
--#24441 2022.06.18 MOD END
order by mc.DTS_YM, mc.CD_CUSTOMER
