<?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">
	 
<!-- 해당 부분의 namespace는 project package + Mapper Package + Mapper Interface 이름입니다. -->
<mapper namespace="com.elt.ems.mapper.DataTableMapper">
	
    <sql id="commonPagingHeader"  >
      SELECT R1.* FROM (
    </sql>
    
    <sql id="commonPagingFooter"  >
      ) R1 LIMIT #{pgtl.startNo}, #{pgtl.listPerPage}
    </sql>   
   		 
	<select id="selectList" parameterType="com.elt.ems.vo.DataTableVo"  resultType="com.elt.ems.vo.DataTableVo">

		select *
		from t_data_table_statistics 
		where inputYear = #{inputYear} 
		<if test="inputMonth != null and inputMonth != '' "> and inputMonth = #{inputMonth} </if>
		order by inputMonth desc, inputYear desc, tableName asc	
     
  	</select>   
  	
  	
	<select id="listDataQueryCount" parameterType="com.elt.ems.vo.SearchMapVo"  resultType="int">
		select  count(*) as count from (
			${map.queryString}	   
		) a   
  	</select>  
  	  	
	<select id="listDataQuery" parameterType="com.elt.ems.vo.SearchMapVo"  resultType="com.elt.ems.common.CaseSensibleHashMap">

	  <if test="pgtl != null">
        <include refid="commonPagingHeader" /> 
      </if>
		${map.queryString}	      
      <if test="pgtl != null">
        <include refid="commonPagingFooter" />
      </if>		
     
  	</select>  
  	
	<select id="listDataInspectMonth" parameterType="com.elt.ems.vo.DataTableVo"  resultType="com.elt.ems.common.CaseSensibleHashMap">

		select s2.*, CEIL((s2.dataSize - s1.dataSize)/1000) as dataSizeDiff, (s2.dataRow - s1.dataRow) as dataRowDiff
		from (
			select s2.* 
			from t_data_table_statistics s2,
			(
				select tableName, max(dataSize/1000) as dataSizeMax
				from t_data_table_statistics
				where inputYear=#{inputYear} and inputMonth=#{inputMonth}
				group by tableName
				order by dataSizeMax desc
				LIMIT 0, 20
			) s0 
			where s2.tableName = s0.tableName
			and s2.inputYear=#{inputYear}
		) s2 left join t_data_table_statistics s1
		on s2.inputYear = s1.inputYear
		and s2.inputMonth = (s1.inputMonth+1)
		and s2.tableName = s1.tableName
		<if test="orderby == null"> order by s2.inputMonth asc, s2.tableName asc </if>
		<if test="orderby != null"> order by s2.tableName asc, s2.inputMonth asc </if>
		
     
  	</select>   	
  		   		
</mapper>