[load_grdList]
select
--	 w2.tan_kind
--	,w2.tan_cd
--	,w2.tan_nm
	 h1.koumoku1 as tan_kind
	,mt.tan_cd
	,mt.tan_nm
	,(case when(w2.end_time is not null)then(to_char(w2.end_time,'yyyymmdd'))else(null)end) as prev_end_ymd
	,w.anken_cd
	,w.anken_nm
	,to_char(w.stt_time,'yyyymmdd') as start_ymd
	,to_char(w.end_time,'yyyymmdd') as end_ymd
--2018.12.18 MOD STT
--	,w.over_cnt
	,COALESCE(w.over_cnt,0) as over_cnt
--2018.12.18 MOD END
from m_tanto mt
inner join m_hanyo h1 on (h1.mst_kbn = '55' and h1.han_cd = mt.bu_cd)
left join (
	select 
		  v.tan_kind
		, v.tan_cd as tan_cd
		, v.tan_nm as tan_nm
		, max((case when(to_char(v.end_time,'yyyymmdd') < ':base_ymd')then(v.end_time)else(null)end)) as end_time
	from v_tan_anken_kikan v
	where 1 = 1
	group by v.tan_kind, v.tan_cd, v.tan_nm

) w2 on (w2.tan_kind = h1.koumoku1 and w2.tan_cd = mt.tan_cd)
left join (
	select 
		  v.tan_kind
		, v.tan_cd as tan_cd
		, v.anken_cd as anken_cd
		, v.anken_nm as anken_nm
		, v.stt_time as stt_time
		, v.end_time as end_time
		, (select count(*) from v_tan_anken_kikan v2 where v2.tan_cd = v.tan_cd and v2.anken_cd != v.anken_cd and v.stt_time between v2.stt_time and v2.end_time) as over_cnt
	from v_tan_anken_kikan v
	where 1 = 1
	and to_char(v.end_time,'yyyymmdd') >= ':base_ymd'
) w on (w.tan_kind = w2.tan_kind and w.tan_cd = w2.tan_cd)
where 1 = 1
and mt.del_flg = 0
and h1.koumoku1 != ''
and (('' = ':tan_cd') or (mt.tan_cd = ':tan_cd'))
and ((w.anken_cd = ':anken_cd') or ('' = ':anken_cd'))

order by tan_cd, start_ymd, anken_cd




