<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.dbnt.faisp.equip.mapper.EquipMapper">
	
	<select id="selectEquipStatus" resultType="com.dbnt.faisp.util.ParamMap" parameterType="Equip">
	    select category_cd,
			   equ_type,
			   item_value,
			   item_cd,
			     sum( cnt_bon+ 
					  cnt_center+
					  cnt_incheon+
					  cnt_pyeongtaek+
					  cnt_taean+
					  cnt_boryeong+
					  cnt_west+
					  cnt_mokpo+
					  cnt_buan+
					  cnt_gunsan+
					  cnt_yusu+
					  cnt_wando+
					  cnt_south+
					  cnt_ulsan+
					  cnt_busan+
					  cnt_changwon+
					  cnt_tongyong+
					  cnt_sacheon+
					  cnt_east+
					  cnt_sokcho+
					  cnt_donghe+
					  cnt_ulgin+
					  cnt_pohang+
					  cnt_jeju+
					  cnt_jejuseo+
					  cnt_seoguipo) as total,
			   sum(cnt_bon) as cnt_bon,
			   sum(cnt_center) as cnt_center,
			   sum(cnt_incheon) as cnt_incheon,
			   sum(cnt_pyeongtaek) as cnt_pyeongtaek,
			   sum(cnt_taean) as cnt_taean,
			   sum(cnt_boryeong) as cnt_boryeong,
			   sum(cnt_west) as cnt_west,
			   sum(cnt_mokpo) as cnt_mokpo,
			   sum(cnt_buan) as cnt_buan,
			   sum(cnt_gunsan) as cnt_gunsan,
			   sum(cnt_yusu) as cnt_yusu,
			   sum(cnt_wando) as cnt_wando,
			   sum(cnt_south) as cnt_south,
			   sum(cnt_ulsan) as cnt_ulsan,
			   sum(cnt_busan) as cnt_busan,
			   sum(cnt_changwon) as cnt_changwon,
			   sum(cnt_tongyong) as cnt_tongyong,
			   sum(cnt_sacheon) as cnt_sacheon,
			   sum(cnt_east) as cnt_east,
			   sum(cnt_sokcho) as cnt_sokcho,
			   sum(cnt_donghe) as cnt_donghe,
			   sum(cnt_ulgin) as cnt_ulgin,
			   sum(cnt_pohang) as cnt_pohang,
			   sum(cnt_jeju) as cnt_jeju,
			   sum(cnt_jejuseo) as cnt_jejuseo,
			   sum(cnt_seoguipo) as cnt_seoguipo
		from(
				select a.category_cd,
					   a.item_cd,
							    	   (select item_value from code_mgt cmt where a.category_cd = cmt.item_cd) as equ_type,
									   a.item_value,
									   case 
									   	  when b.mgt_organ = 'OG001' then b.item_qty
									   	  else 0
									   end as cnt_bon,
									   case 
									   	  when b.mgt_organ = 'OG002' then b.item_qty
									   	  else 0
									   end as cnt_center,
									   case 
									   	  when b.mgt_organ = 'OG007' then b.item_qty
									   	  else 0
									   end as cnt_incheon,
									   case 
									   	  when b.mgt_organ = 'OG008' then b.item_qty
									   	  else 0
									   end as cnt_pyeongtaek,
									   case 
									   	  when b.mgt_organ = 'OG009' then b.item_qty
									   	  else 0
									   end as cnt_taean,
									   case 
									   	  when b.mgt_organ = 'OG010' then b.item_qty
									   	  else 0
									   end as cnt_boryeong,
									   case 
									   	  when b.mgt_organ = 'OG003' then b.item_qty
									   	  else 0
									   end as cnt_west,
									   case 
									   	  when b.mgt_organ = 'OG011' then b.item_qty
									   	  else 0
									   end as cnt_mokpo,
									   case 
									   	  when b.mgt_organ = 'OG012' then b.item_qty
									   	  else 0
									   end as cnt_buan,
									   case 
									   	  when b.mgt_organ = 'OG013' then b.item_qty
									   	  else 0
									   end as cnt_gunsan,
									   case 
									   	  when b.mgt_organ = 'OG014' then b.item_qty
									   	  else 0
									   end as cnt_yusu,
									   case 
									   	  when b.mgt_organ = 'OG015' then b.item_qty
									   	  else 0
									   end as cnt_wando,
									   case 
									   	  when b.mgt_organ = 'OG004' then b.item_qty
									   	  else 0
									   end as cnt_south,
									   case 
									   	  when b.mgt_organ = 'OG016' then b.item_qty
									   	  else 0
									   end as cnt_ulsan,
									   case 
									   	  when b.mgt_organ = 'OG017' then b.item_qty
									   	  else 0
									   end as cnt_busan,
									   case 
									   	  when b.mgt_organ = 'OG018' then b.item_qty
									   	  else 0
									   end as cnt_changwon,
									   case 
									   	  when b.mgt_organ = 'OG019' then b.item_qty
									   	  else 0
									   end as cnt_tongyong,
									   case 
									   	  when b.mgt_organ = 'OG020' then b.item_qty
									   	  else 0
									   end as cnt_sacheon,
									   case 
									   	  when b.mgt_organ = 'OG005' then b.item_qty
									   	  else 0
									   end as cnt_east,
									   case 
									   	  when b.mgt_organ = 'OG021' then b.item_qty
									   	  else 0
									   end as cnt_sokcho,
									   case 
									   	  when b.mgt_organ = 'OG022' then b.item_qty
									   	  else 0
									   end as cnt_donghe,
									   case 
									   	  when b.mgt_organ = 'OG023' then b.item_qty
									   	  else 0
									   end as cnt_ulgin,
									   case 
									   	  when b.mgt_organ = 'OG024' then b.item_qty
									   	  else 0
									   end as cnt_pohang,
									   case 
									   	  when b.mgt_organ = 'OG006' then b.item_qty
									   	  else 0
									   end as cnt_jeju,
									    case 
									   	  when b.mgt_organ = 'OG025' then b.item_qty
									   	  else 0
									   end as cnt_jejuseo,
									    case 
									   	  when b.mgt_organ = 'OG026' then b.item_qty
									   	  else 0
									   end as cnt_seoguipo
								from (select category_cd,
											   item_cd,
											   item_value
										from code_mgt
										where category_cd in('IT1','IT2','IT3','IT4')
										order by category_cd,item_cd asc) a left outer join
										(select a.equ_key,
												   a.version_no,
												   mgt_organ,
												   equ_type,
												   detail_type,
												   item_qty
											from equ_mgt a,
												 (select equ_key,max(version_no) as lastVer 
												  from equ_mgt group by equ_key) b,
												  code_mgt cm
											where a.equ_key = b.equ_key
											and a.version_no = b.lastVer
											group by a.equ_key,a.version_no) b on a.item_cd = b.detail_type
								order by a.category_cd,a.item_cd asc
					) c
		group by c.category_cd,c.equ_type,c.item_value,item_cd
		order by category_cd,item_cd asc
  </select>

 
</mapper>