Files
PollPredict/src/main/resources/mapper/analysis_mapper.xml
2026-05-22 15:06:49 +09:00

1046 lines
52 KiB
XML

<?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="kr.co.kr.dao.AnalysisDao">
<resultMap id="AnalysisVo" type="kr.co.kr.vo.AnalysisVo"></resultMap>
<resultMap id="AnalysisVo1" type="kr.co.kr.vo.AnalysisVo"></resultMap>
<resultMap id="AnalysisVo2" type="kr.co.kr.vo.AnalysisVo"></resultMap>
<resultMap id="AnalysisVo3" type="kr.co.kr.vo.AnalysisVo"></resultMap>
<resultMap id="AnalysisVo4" type="kr.co.kr.vo.AnalysisVo"></resultMap>
<resultMap id="AnalysisVo5" type="kr.co.kr.vo.AnalysisVo"></resultMap>
<select id="getArea" resultMap="AnalysisVo">
SELECT SD.sido_cd, SD.sido_nm,
AR.area_cd, AR.area_nm
FROM exitpoll.dbo.F_sido SD LEFT JOIN exitpoll.dbo.F_area AR
ON SD.sido_cd = AR.sido_cd
WHERE AR.company = 'KRI'
ORDER BY SD.sido_cd, AR.area_cd
</select>
<select id="getTimeBand" resultMap="AnalysisVo">
SELECT * FROM exitpoll.dbo.F_TIME WHERE time_band > '0'
</select>
<select id="getWeight" resultMap="AnalysisVo">
SELECT area_cd, cluster, poll_pre, poll_5p, poll_5n
FROM exitpoll.dbo.F_weight
</select>
<select id="getOption" resultMap="AnalysisVo1,AnalysisVo2">
SELECT vote_cd, option_cd, option_nm ,ROW_NUMBER() OVER (ORDER BY CAST(vote_cd AS INT), CAST(sort AS INT)) AS order_no
FROM exitpoll.dbo.F_option
ORDER BY CAST(vote_cd AS INT), CAST(option_type AS INT), sort
SELECT area_cd, vote_cd, cand_option_cd, party_option_cd, edu_option_cd
FROM exitpoll.dbo.F_output_option
ORDER BY area_cd, vote_cd
</select>
<update id="optionSave">
UPDATE exitpoll.dbo.F_output_option
<if test="f_cand_party == 'CAND'">
set cand_option_cd = #{option_cd}
</if>
<if test="f_cand_party == 'PARTY'">
set party_option_cd = #{option_cd}
</if>
<if test="f_cand_party == 'EDU'">
set edu_option_cd = #{option_cd}
</if>
WHERE area_cd = #{area_cd} AND vote_cd = #{vote_cd}
</update>
<select id="getOutputData" resultMap="AnalysisVo1,AnalysisVo2,AnalysisVo3,AnalysisVo4,AnalysisVo,AnalysisVo5">
SET NOCOUNT ON
-- @@@@@ 1. 테이블 F_TMP_EXPOLL을 비롯한 템프 테이블들을 Drop한다
DROP TABLE IF EXISTS exitpoll.dbo.F_TMP_EXPOLL
DROP TABLE IF EXISTS exitpoll.dbo.TMP_EXIT_POLL
DROP TABLE IF EXISTS exitpoll.dbo.TMP_GENDER_AGE
DROP TABLE IF EXISTS exitpoll.dbo.TMP_GENDER_AGE_RATE
DROP TABLE IF EXISTS exitpoll.dbo.TMP_GENDER_AGE_SUM
DROP TABLE IF EXISTS exitpoll.dbo.TMP_CLUSTER_POLL
DROP TABLE IF EXISTS exitpoll.dbo.TMP_CLUSTER_GA_SUM
DROP TABLE IF EXISTS exitpoll.dbo.TMP_CLUSTER_DATA
DROP TABLE IF EXISTS exitpoll.dbo.TMP_WEIGHT_GA
DROP TABLE IF EXISTS exitpoll.dbo.TMP_PRE_P5N_CNT
DROP TABLE IF EXISTS exitpoll.dbo.TMP_PRE_P5N_RATIO;
-- @@@@@ 2. F_EXITPOLL에서 선택된 차수 보다 작거나 같은 Data를 F_TMP_EXPOLL에 넣는다
SELECT a.* INTO exitpoll.dbo.F_TMP_EXPOLL
FROM exitpoll.dbo.F_EXITPOLL a
left join exitpoll.dbo.F_cand c on a.area_cd=c.area_cd and a.cand_cd=c.cand_cd and c.quit = 0
<!-- WHERE #{time_band} >= a.time_band AND a.time_band != '0' and c.cand_cd is not null -->
WHERE #{time_band} >= a.time_band AND a.time_band != '0'
and (a.cand_cd = '00' or c.cand_cd is not null) -- 무응답도 조회
<!-- and a.party_cd not in ('01','02') and (a.cand_cd = '00' or c.cand_cd is not null) -->
<!-- and reg_date <![CDATA[<=]]> '2024-04-10 16:54:59.999' -->
-- @@@@@ F_TMP_EXPOLL 기준으로 지역 X 투표소 X 성 X 연령 X 후보(정당)의 count값을 임시 테이블 TMP_EXIT_POLL에 넣는다
SELECT B.area_cd, B.subarea_cd,
B.gender, B.age, cand_party,
ISNULL(T.cand_party_cd, B.cand_party_cd) AS cand_party_cd, ISNULL(T.cnt, 0) AS cnt
INTO exitpoll.dbo.TMP_EXIT_POLL
FROM
(
SELECT A.area_cd, GA.gender, GA.age, SA.subarea_cd, CP.cand_party, CP.cand_party_cd
FROM exitpoll.dbo.F_area A LEFT JOIN exitpoll.dbo.F_subarea SA
ON A.area_cd = SA.area_cd LEFT JOIN (
SELECT 'CAND' AS cand_party, area_cd, cand_cd AS cand_party_cd
FROM exitpoll.dbo.F_cand UNION ALL
SELECT 'PARTY' AS cand_party, '0000' AS area_cd, party_cd AS cand_party_cd
FROM exitpoll.dbo.F_party WHERE party_yn = '1' UNION ALL
SELECT 'EDU' AS cand_party, area_cd, edu_cd AS cand_party_cd
FROM exitpoll.dbo.F_edu WHERE quit = '0' UNION ALL
SELECT 'CAND' AS cand_party, '0000' AS area_cd, '00' AS cand_party_cd UNION ALL
SELECT 'PARTY' AS cand_party, '0000' AS area_cd, '00' AS cand_party_cd UNION ALL
SELECT 'EDU' AS cand_party, '0000' AS area_cd, '00' AS cand_party_cd
) CP ON (A.area_cd = CP.area_cd OR CP.area_cd = '0000') AND
CP.cand_party =
<if test="f_cand_party == 'CAND'">
'CAND'
</if>
<if test="f_cand_party == 'PARTY'">
'PARTY'
</if>
<if test="f_cand_party == 'EDU'">
'EDU'
</if>
LEFT JOIN
(
SELECT CONVERT(varchar(1), number % 3) AS gender, CONVERT(varchar(1), number / 3) AS age
FROM master..spt_values
WHERE type = 'P' and 21 > number
) GA ON 1=1
WHERE A.company = 'KRI'
<if test="f_local == 'CHOICE'">
AND A.area_cd = #{area_cd}
</if>
) B LEFT JOIN
(
SELECT area_cd, subarea_cd, gender, age,
<if test="f_cand_party == 'CAND'">
cand_cd
</if>
<if test="f_cand_party == 'PARTY'">
party_cd
</if>
<if test="f_cand_party == 'EDU'">
edu_cd
</if>
AS cand_party_cd, count(*) AS cnt
FROM exitpoll.dbo.F_TMP_EXPOLL
WHERE 1=1
<if test="f_local == 'CHOICE'">
AND area_cd = #{area_cd}
</if>
GROUP BY area_cd, subarea_cd, gender, age,
<if test="f_cand_party == 'CAND'">
cand_cd
</if>
<if test="f_cand_party == 'PARTY'">
party_cd
</if>
<if test="f_cand_party == 'EDU'">
edu_cd
</if>
) T ON B.area_cd = T.area_cd AND B.subarea_cd = T.subarea_cd
AND B.cand_party_cd = T.cand_party_cd
AND B.gender = T.gender AND B.age = T.age
<if test="f_local == 'CHOICE'">
WHERE B.area_cd = #{area_cd}
</if>
-- @@@@@ 4. TMP_EXIT_POLL 기준으로 지역 X 투표소 X 성 X 연령 X 후보(정당)의 sum(count)값을 임시 테이블 TMP_GENDER_AGE에 넣는다
-- 선거구 X 층 - 계층별 집계 자료
-- DROP TABLE TMP_GENDER_AGE
SELECT TEP.area_cd, cluster, cluster_nm, gender, age, TEP.cand_party, TEP.cand_party_cd, SUM(cnt) AS cnt
INTO exitpoll.dbo.TMP_GENDER_AGE
FROM
exitpoll.dbo.TMP_EXIT_POLL TEP LEFT JOIN exitpoll.dbo.F_subarea SA
ON TEP.area_cd = SA.area_cd AND TEP.subarea_cd = SA.subarea_cd
<if test="f_cand_party == 'CAND'">
LEFT JOIN exitpoll.dbo.F_cand C ON TEP.area_cd = C.area_cd AND TEP.cand_party_cd = C.cand_cd
</if>
<if test="f_cand_party == 'EDU'">
LEFT JOIN exitpoll.dbo.F_edu E ON TEP.area_cd = E.area_cd AND TEP.cand_party_cd = E.edu_cd AND E.quit = '0'
</if>
LEFT JOIN exitpoll.dbo.F_party P ON
<if test="f_cand_party == 'CAND'"> C.party_cd </if>
<if test="f_cand_party == 'PARTY'"> TEP.cand_party_cd </if>
<if test="f_cand_party == 'EDU'"> '99' </if>
= P.party_cd
WHERE (TEP.cand_party_cd = '00' OR P.party_nm IS NOT NULL)
GROUP BY TEP.area_cd, cluster, cluster_nm, gender, age, TEP.cand_party, TEP.cand_party_cd
-- @@@@@ 5. TMP_GENDER_AGE 기준으로 지역 X 층 X 후보(정당) sum(count)를 임시 테이블 TMP_GENDER_AGE_SUM 에 넣는다
-- 선거구 X 층 - 계층 응답자 집계합 (계층 무응답자 배분용)
-- DROP TABLE TMP_GENDER_AGE_SUM
SELECT area_cd, cluster, cluster_nm, cand_party_cd, SUM(cnt) AS cnt
INTO exitpoll.dbo.TMP_GENDER_AGE_SUM
FROM exitpoll.dbo.TMP_GENDER_AGE
WHERE gender != '0' AND age != '0'
GROUP BY area_cd, cluster, cluster_nm, cand_party_cd
-- @@@@@ 6. TMP_GENDER_AGE와 TMP_GENDER_AGE_RATE을 사용하여 지역 X 층 X 성 X 연령 X 후보(정당)의 %값을 만들어 TMP_GENDER_AGE_RATE에 넣는다
-- 선거구 X 층 - 계층 응답자 득표율 (계층 무응답자 배분용)
-- DROP TABLE TMP_GENDER_AGE_RATE
SELECT GA.area_cd, GA.cluster, GA.cluster_nm, gender, age, GA.cand_party_cd,
CASE SGA.cnt WHEN 0 THEN 0.0 ELSE GA.cnt * 1.0 / SGA.cnt END AS vote_rate
INTO exitpoll.dbo.TMP_GENDER_AGE_RATE
FROM exitpoll.dbo.TMP_GENDER_AGE GA LEFT JOIN exitpoll.dbo.TMP_GENDER_AGE_SUM SGA
ON GA.area_cd = SGA.area_cd AND GA.cluster = SGA.cluster AND GA.cand_party_cd = SGA.cand_party_cd
WHERE gender != '0' AND age != '0'
-- @@@@@ 7. TMP_GENDER_AGE와 TMP_GENDER_AGE_RATE를 사용하여 성/연령 응답값이 있는 케이스들과 그렇지 않은 케이스들의 통합 테이블 TMP_CLUSTER_POLL을 만든다,
-- 성/연령 응답값이 둘다 없으면 해당 층의 해당 후보(정당)의 지지도를 곱한다, 성이 있으면 성 별 후보(정당) 지지도를 곱한다, 연령이 있으면 연령 별 후보(정당) 지지도를 곱한다
-- 선거구 X 층 - 계층 무응답자 배분 후 득표수
SELECT area_cd, cluster, cluster_nm, gender, age, cand_party, cand_party_cd, sum(cnt) AS cnt
INTO exitpoll.dbo.TMP_CLUSTER_POLL
FROM
(
/* 성/연령 응답자 */
SELECT area_cd, cluster, cluster_nm, gender, age, cand_party, cand_party_cd, cnt
FROM exitpoll.dbo.TMP_GENDER_AGE
WHERE gender != '0' AND age != '0'
/* 성/연령 무응답자 배분 */ UNION ALL
SELECT GA.area_cd, GA.cluster, GA.cluster_nm, GAR.gender, GAR.age, cand_party, GA.cand_party_cd, (GA.cnt * vote_rate) AS calc_cnt
FROM exitpoll.dbo.TMP_GENDER_AGE GA LEFT JOIN exitpoll.dbo.TMP_GENDER_AGE_RATE GAR
ON GA.area_cd = GAR.area_cd AND GA.cluster = GAR.cluster AND GA.cand_party_cd = GAR.cand_party_cd
WHERE GA.gender = '0' AND GA.age = '0'
/* 성 무응답자 배분 */ UNION ALL
SELECT GA.area_cd, GA.cluster, GA.cluster_nm, GAR.gender, GA.age, cand_party, GA.cand_party_cd, (GA.cnt * SUM(vote_rate)) AS calc_cnt
FROM exitpoll.dbo.TMP_GENDER_AGE GA LEFT JOIN exitpoll.dbo.TMP_GENDER_AGE_RATE GAR
ON GA.area_cd = GAR.area_cd AND GA.cluster = GAR.cluster AND GA.cand_party_cd = GAR.cand_party_cd
WHERE GA.gender = '0' AND GA.age != '0'
GROUP BY GA.area_cd, GA.cluster, GA.cluster_nm, GAR.gender, GA.age, cand_party, GA.cand_party_cd, GA.cnt
/* 연령 무응답자 배분 */ UNION ALL
SELECT GA.area_cd, GA.cluster, GA.cluster_nm, GA.gender, GAR.age, cand_party, GA.cand_party_cd, (GA.cnt * SUM(vote_rate)) AS calc_cnt
FROM exitpoll.dbo.TMP_GENDER_AGE GA LEFT JOIN exitpoll.dbo.TMP_GENDER_AGE_RATE GAR
ON GA.area_cd = GAR.area_cd AND GA.cluster = GAR.cluster AND GA.cand_party_cd = GAR.cand_party_cd
WHERE GA.gender != '0' AND GA.age = '0'
GROUP BY GA.area_cd, GA.cluster, GA.cluster_nm, GA.gender, GAR.age, cand_party, GA.cand_party_cd, GA.cnt
) T
GROUP BY area_cd, cluster, cluster_nm, gender, age, cand_party, cand_party_cd
-- @@@@@ 8. TMP_CLUSTER_POLL를 기준으로 지역 X 층 X 성 X 연령의 sum(count)를 임시 테이블 TMP_CLUSTER_GA_SUM에 넣는다
-- 선거구 X 층 - 계층 응답자 집계합 (계층별 득표율 구하기 위함)
SELECT area_cd, cluster, cluster_nm, gender, age, sum(cnt) AS cnt
INTO exitpoll.dbo.TMP_CLUSTER_GA_SUM
FROM exitpoll.dbo.TMP_CLUSTER_POLL
GROUP BY area_cd, cluster, cluster_nm, gender, age
-- @@@@@ 9. TMP_CLUSTER_GA_SUM, TMP_CLUSTER_POLL을 사용하여 지역 X 층 X 계층 X 후보(정당) 득표수, 계층별 득표수 합을 TMP_CLUSTER_DATA에 넣는다
-- 보고서 2 계층별 자료
-- 선거구 X 층 - 계층별 득표수, 계층별 득표수 합, 계층별 득표율
SELECT area_cd, cluster, cluster_nm, sect, cand_party, cand_party_cd, sum_cnt, cnt
INTO exitpoll.dbo.TMP_CLUSTER_DATA
FROM
(
/* 해당 층의 전체 득표수, 후보(정당)별 득표수 */
SELECT CGS.area_cd, CGS.cluster, CGS.cluster_nm,
'00_전체' AS sect,
cand_party, CP.cand_party_cd, CP.cnt, CGS.cnt AS sum_cnt
FROM
(
SELECT area_cd, cluster, cluster_nm, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_CLUSTER_GA_SUM
GROUP BY area_cd, cluster, cluster_nm
) CGS LEFT JOIN
(
SELECT area_cd, cluster, cluster_nm, cand_party, cand_party_cd, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_CLUSTER_POLL
GROUP BY area_cd, cluster, cluster_nm, cand_party, cand_party_cd
) CP
ON CGS.area_cd = CP.area_cd AND CGS.cluster = CP.cluster
/* 해당 층의 성 계층 득표수, 후보(정당)별 득표수 */ UNION ALL
SELECT CGS.area_cd, CGS.cluster, CGS.cluster_nm,
CASE CGS.gender WHEN '1' THEN '10_남자' WHEN '2' THEN '20_여자' END AS sect,
cand_party, CP.cand_party_cd, CP.cnt, CGS.cnt AS sum_cnt
FROM
(
SELECT area_cd, cluster, cluster_nm, gender, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_CLUSTER_GA_SUM
GROUP BY area_cd, cluster, cluster_nm, gender
) CGS LEFT JOIN
(
SELECT area_cd, cluster, cluster_nm, gender, cand_party, cand_party_cd, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_CLUSTER_POLL
GROUP BY area_cd, cluster, cluster_nm, gender, cand_party, cand_party_cd
) CP
ON CGS.area_cd = CP.area_cd AND CGS.cluster = CP.cluster AND CGS.gender = CP.gender
/* 해당 층의 연령 계층 득표수, 후보(정당)별 득표수 */ UNION ALL
SELECT CGS.area_cd, CGS.cluster, CGS.cluster_nm,
CASE CGS.age WHEN '1' THEN '01_20대'
WHEN '2' THEN '02_30대'
WHEN '3' THEN '03_40대'
WHEN '4' THEN '04_50대'
WHEN '5' THEN '05_60대'
WHEN '6' THEN '06_70세이상'
END AS sect,
cand_party, CP.cand_party_cd, CP.cnt, CGS.cnt AS sum_cnt
FROM
(
SELECT area_cd, cluster, cluster_nm, age, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_CLUSTER_GA_SUM
GROUP BY area_cd, cluster, cluster_nm, age
) CGS LEFT JOIN
(
SELECT area_cd, cluster, cluster_nm, age, cand_party, cand_party_cd, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_CLUSTER_POLL
GROUP BY area_cd, cluster, cluster_nm, age, cand_party, cand_party_cd
) CP
ON CGS.area_cd = CP.area_cd AND CGS.cluster = CP.cluster AND CGS.age = CP.age
/* 해당 층의 성/연령 계층 득표수, 후보(정당)별 득표수 */ UNION ALL
SELECT CGS.area_cd, CGS.cluster, CGS.cluster_nm,
CASE CGS.gender
WHEN '1' THEN
CASE CGS.age WHEN '1' THEN '11_20대 남'
WHEN '2' THEN '12_30대 남'
WHEN '3' THEN '13_40대 남'
WHEN '4' THEN '14_50대 남'
WHEN '5' THEN '15_60대 남'
WHEN '6' THEN '16_70세이상 남' END
WHEN '2' THEN
CASE CGS.age WHEN '1' THEN '21_20대 여'
WHEN '2' THEN '22_30대 여'
WHEN '3' THEN '23_40대 여'
WHEN '4' THEN '24_50대 여'
WHEN '5' THEN '25_60대 여'
WHEN '6' THEN '26_70세이상 여' END
END AS sect,
cand_party, CP.cand_party_cd, CP.cnt, CGS.cnt AS sum_cnt
FROM
(
SELECT area_cd, cluster, cluster_nm, gender, age, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_CLUSTER_GA_SUM
GROUP BY area_cd, cluster, cluster_nm, gender, age
) CGS LEFT JOIN
(
SELECT area_cd, cluster, cluster_nm, gender, age, cand_party, cand_party_cd, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_CLUSTER_POLL
GROUP BY area_cd, cluster, cluster_nm, gender, age, cand_party, cand_party_cd
) CP
ON CGS.area_cd = CP.area_cd AND CGS.cluster = CP.cluster AND CGS.gender = CP.gender AND CGS.age = CP.age
) T
-- @@@@@ 10. F_weight테이블로 부터 증 X 계층별 - 사전투표, 5시이후 가중치를 정리하여 TMP_WEIGHT_GA에 넣는다
-- sect = 11_20대 남, 12_30대 남, 13_40대 남, 14_50대 남, 16_70세 이상 남
-- sect = 21_20대 여, 22_30대 여, 23_40대 여, 24_50대 여, 26_70세 이상 여
-- 선거구 X 계층별 - 사전투표, 5시이후 가중치
SELECT area_cd, cluster,
RIGHT('0' + CONVERT(varchar(2),
CONVERT(integer, SUBSTRING(pre_ga, 5, 1)) +
CASE RIGHT(pre_ga, 1)
WHEN 'm' THEN 9
WHEN 'w' THEN 19 END), 2) AS sect,
CONVERT(NUMERIC(12, 10), pre_ratio) AS pre_ratio,
CONVERT(NUMERIC(12, 10), p5n_ratio) AS p5n_ratio
INTO exitpoll.dbo.TMP_WEIGHT_GA
FROM
(
SELECT area_cd, cluster,
-- 사전투표 20대 남 ~ 70 남 가중치
pre_20_m, pre_30_m, pre_40_m, pre_50_m, pre_60_m, pre_70_m,
-- 사전투표 20대 여 ~ 70 여 가중치
pre_20_w, pre_30_w, pre_40_w, pre_50_w, pre_60_w, pre_70_w,
-- 5시이후 20대 남 ~ 70 남 가중치
p5n_20_m, p5n_30_m, p5n_40_m, p5n_50_m, p5n_60_m, p5n_70_m,
-- 5시이후 20대 여 ~ 70 여 가중치
p5n_20_w, p5n_30_w, p5n_40_w, p5n_50_w, p5n_60_w, p5n_70_w
FROM exitpoll.dbo.F_weight
) T
UNPIVOT
(
pre_ratio FOR pre_ga IN (
pre_20_m, pre_30_m, pre_40_m, pre_50_m, pre_60_m, pre_70_m,
pre_20_w, pre_30_w, pre_40_w, pre_50_w, pre_60_w, pre_70_w
)
) U1
UNPIVOT
(
p5n_ratio FOR p5n_ga IN (
p5n_20_m, p5n_30_m, p5n_40_m, p5n_50_m, p5n_60_m, p5n_70_m,
p5n_20_w, p5n_30_w, p5n_40_w, p5n_50_w, p5n_60_w, p5n_70_w
)
) U2
WHERE RIGHT(pre_ga, 4) = RIGHT(p5n_ga, 4)
-- @@@@@ 11. TMP_WEIGHT_GA의 가중치를 TMP_CLUSTER_DATA의 득표율을 기준으로 선거구 X 층 X 계층의 사전, 5시 이후 가중치 계산해 TMP_PRE_P5N_RATIO에 넣는다
-- 사전, 5시 이후 가중치 계산
------------!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!with java, it's not working well need to be fixed
SELECT A.area_cd, A.cluster, A.sect,
CASE A.cnt WHEN 0 THEN 0.0 ELSE WGA.pre_ratio / (A.cnt / B.sum_cnt) END AS pre_ratio,
CASE A.cnt WHEN 0 THEN 0.0 ELSE WGA.p5n_ratio / (A.cnt / B.sum_cnt) END AS p5n_ratio
INTO exitpoll.dbo.TMP_PRE_P5N_RATIO
FROM
(
SELECT area_cd, cluster, LEFT(sect, 2) AS sect, SUM(cnt) AS cnt
FROM exitpoll.dbo.TMP_CLUSTER_DATA
WHERE (LEFT(sect, 2) BETWEEN '11' AND '16' OR LEFT(sect, 2) BETWEEN '21' AND '26') AND cand_party_cd != '00'
GROUP BY area_cd, cluster, LEFT(sect, 2)
) A LEFT JOIN (
SELECT area_cd, cluster, SUM(cnt) AS sum_cnt
FROM exitpoll.dbo.TMP_CLUSTER_DATA
WHERE LEFT(sect, 2) = '00' AND cand_party_cd != '00'
GROUP BY area_cd, cluster
) B ON A.area_cd = B.area_cd AND A.cluster = B.cluster LEFT JOIN exitpoll.dbo.TMP_WEIGHT_GA WGA
ON A.area_cd = WGA.area_cd AND A.cluster = WGA.cluster AND A.sect = WGA.sect
-- @@@@@ 12. TMP_CLUSTER_DATA의 득표수에 계산된 TMP_PRE_P5N_RATIO의 가중치를 적용해서 TMP_PRE_P5N_CNT에 넣는다
-- 성연령별 가중치 적용 (사전, 5시 이후)
SELECT A.area_cd, A.cluster, A.cand_party, A.cand_party_cd, A.sect, A.sect_str, sum_cnt, n_cnt, cnt,
(sum_cnt-n_cnt) * B.pre_ratio AS pre_sum, cnt * B.pre_ratio AS pre_cnt,
(sum_cnt-n_cnt) * B.p5n_ratio AS p5n_sum, cnt * B.p5n_ratio AS p5n_cnt
INTO exitpoll.dbo.TMP_PRE_P5N_CNT
FROM
(
SELECT area_cd, cluster, cand_party, LEFT(sect, 2) AS sect, sect AS sect_str, cand_party_cd, sum_cnt, cnt
FROM exitpoll.dbo.TMP_CLUSTER_DATA
WHERE (LEFT(sect, 2) BETWEEN '11' AND '16' OR LEFT(sect, 2) BETWEEN '21' AND '26') AND cand_party_cd != '00'
) A LEFT JOIN exitpoll.dbo.TMP_PRE_P5N_RATIO B ON A.area_cd = B.area_cd AND A.cluster = B.cluster AND A.sect = B.sect
LEFT JOIN
(
SELECT area_cd, cluster, LEFT(sect, 2) AS sect, sect AS sect_str, cnt AS n_cnt
FROM exitpoll.dbo.TMP_CLUSTER_DATA
WHERE (LEFT(sect, 2) BETWEEN '11' AND '16' OR LEFT(sect, 2) BETWEEN '21' AND '26') AND cand_party_cd = '00'
) C ON A.area_cd = C.area_cd AND A.cluster = C.cluster AND A.sect = C.sect
-- @@@@@ 13. TMP_OUTPUT_DDAY 초기화
DELETE FROM exitpoll.dbo.TMP_OUTPUT_DDAY
WHERE cand_party = #{f_cand_party}
<if test="f_local == 'CHOICE'">
AND area_cd = #{area_cd}
</if>
-- @@@@@ 14. TMP_PRE_P5N_CNT을 사용하여 층 X 후보(정당) X 옵션 별 count를 계산하여 TMP_OUTPUT_DDAY에 넣는다
INSERT INTO exitpoll.dbo.TMP_OUTPUT_DDAY
/*
당일 옵션
01 성연령 보정
02 거절자 제외
03 열세자 95%(성/연령)
04 열세자 90%(성/연령)
05 열세자 85%(성/연령)
06 열세자 80%(성/연령)
*/
SELECT area_cd, cluster, cand_party, sect, cand_party_cd, option_cd, sum_cnt, case when cnt <![CDATA[<]]> 0 then 0 else cnt end as cnt
FROM
(
-- 01. 후보(정당) 무응답 제외 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SELECT area_cd, cluster, cand_party, sect, cand_party_cd, '01' AS option_cd, sum_cnt - n_cnt AS sum_cnt, cnt
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
UNION ALL
-- 01.성연령 보정 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SELECT area_cd, cluster, cand_party, sect, cand_party_cd, '02' AS option_cd, sum_cnt,
CASE cnt WHEN 0 THEN 0 ELSE n_cnt * cnt / (sum_cnt - n_cnt) + cnt END AS cnt
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
UNION ALL
-- 계층 별(성/연령)
-- 03.열세자 95%(성/연령) @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-- 04.열세자 90%(성/연령) @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-- 05.열세자 85%(성/연령) @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-- 06.열세자 80%(성/연령) @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SELECT T.area_cd, T.cluster, T.cand_party, T.sect, T.cand_party_cd, option_cd, sum_cnt,
cnt + CASE sum_cnt - n_cnt WHEN 0 THEN 0 ELSE -- 무응답이 없으니 배분하지 않음
CASE WHEN max_cnt IS NULL THEN -- 단독 1위가 없음( 모든 후보에게 현재 득표 비율대로 n_cnt를 비례배분)
cnt / (sum_cnt - n_cnt) * n_cnt
WHEN max_cnt = cnt THEN -- 단독 1위가 있음( 1위 후보의 n_cnt 목에서 깍음) ex)열세자 95% 인경우 받을게 10이면 9.5만 받음
cnt / (sum_cnt - n_cnt) * n_cnt * weight
ELSE -- 1위가 아닌 후보일 때
CASE WHEN (sum_cnt - n_cnt) - max_cnt = 0 -- 1위를 제외한 나머지 후보들의 현재 표 종합 이 0 인가?
-- 1위에게 주고 남은 표 전부
THEN (n_cnt - (max_cnt / (sum_cnt - n_cnt) * n_cnt * weight))
-- 전체 보정표에서 1위 후보 조정 몫을 뺀 남은 표를, 비1위 후보들 사이의 현재 표 비율대로 후보에게 배분한다
ELSE (n_cnt - (max_cnt / (sum_cnt - n_cnt) * n_cnt * weight)) * cnt / (sum_cnt - n_cnt - max_cnt)
END
END
END AS cnt
FROM exitpoll.dbo.TMP_PRE_P5N_CNT T LEFT JOIN (
SELECT area_cd, cluster, cand_party, sect, cnt AS max_cnt FROM (
SELECT area_cd, cluster, cand_party, sect, cnt,
RANK() OVER(PARTITION BY area_cd, cluster, cand_party, sect ORDER BY cnt DESC) AS rnk
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
) MX
WHERE rnk = 1
GROUP BY area_cd, cluster, cand_party, sect, cnt HAVING sum(rnk) = 1
) MC ON T.area_cd = MC.area_cd AND T.cand_party = MC.cand_party AND T.cluster = MC.cluster AND T.sect = MC.sect LEFT JOIN
(
select weight_rate as weight, option_cd
from exitpoll.dbo.F_option
where vote_cd = 2
and option_type = 4
) W ON 1=1
UNION ALL
-- 계층(성/연령) 통합
-- 07.열세자 95% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-- 08.열세자 90% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-- 09.열세자 85% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-- 10.열세자 80% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SELECT T.area_cd, T.cluster, T.cand_party, '00' AS sect, T.cand_party_cd, option_cd, sum_cnt,
cnt + CASE cnt WHEN 0 THEN 0 ELSE
CASE WHEN max_cnt = cnt THEN
max_cnt / (sum_cnt - n_cnt) * n_cnt * weight
ELSE
(n_cnt - (max_cnt / (sum_cnt - n_cnt) * n_cnt * weight)) * cnt / (sum_cnt - n_cnt - max_cnt)
END
END AS cnt
FROM (
SELECT area_cd, cluster, cand_party, cand_party_cd, sum(sum_cnt) AS sum_cnt, sum(n_cnt) AS n_cnt, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
GROUP BY area_cd, cluster, cand_party, cand_party_cd
) T LEFT JOIN (
SELECT area_cd, cluster, cand_party, cnt AS max_cnt FROM (
SELECT area_cd, cluster, cand_party, sum(cnt) AS cnt,
RANK() OVER(PARTITION BY area_cd, cluster, cand_party ORDER BY sum(cnt) DESC) AS rnk
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
GROUP BY area_cd, cluster, cand_party, cand_party_cd
) MX
WHERE rnk = 1
GROUP BY area_cd, cluster, cand_party, cnt HAVING sum(rnk) = 1
) MC ON T.area_cd = MC.area_cd AND T.cand_party = MC.cand_party AND T.cluster = MC.cluster LEFT JOIN
(
select weight_rate as weight, option_cd
from exitpoll.dbo.F_option
where vote_cd = 2
and option_type = 5
) W ON 1=1
UNION ALL
-- 계층(성/연령) 통합
-- 95% : 지역/층/후보별 득표비율 배분
-- 5% : 지역/후보별 1등 제외, 열세자끼리만 득표비율 배분
-- 11.열세자 전체 95% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-- 12.열세자 전체 90% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-- 13.열세자 전체 85% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-- 14.열세자 전체 80% @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SELECT T.area_cd, T.cluster, T.cand_party, '99' AS sect, T.cand_party_cd, W.option_cd, T.sum_cnt,
T.cnt + ISNULL(T.n_cnt * W.weight * CAST(T.cnt AS DECIMAL(18,10)) / NULLIF(CAST(CL.cluster_total_cnt AS DECIMAL(18,10)), 0) , 0)
+ CASE
WHEN AC3.area_cand_cnt = AC3.max_area_cand_cnt THEN 0
ELSE ISNULL(T.n_cnt * (1.0 - W.weight) * CAST(AC3.area_cand_cnt AS DECIMAL(18,10)) / NULLIF(CAST(AC3.loser_area_total_cnt AS DECIMAL(18,10)), 0) , 0)
END AS cnt
FROM (
SELECT area_cd, cluster, cand_party, cand_party_cd, sum(sum_cnt) AS sum_cnt, sum(n_cnt) AS n_cnt, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
GROUP BY area_cd, cluster, cand_party, cand_party_cd
) T LEFT JOIN (
SELECT area_cd, cluster, cand_party, sum(cnt) AS cluster_total_cnt
FROM (
SELECT area_cd, cluster, cand_party, cand_party_cd, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
GROUP BY area_cd, cluster, cand_party, cand_party_cd
) X
GROUP BY area_cd, cluster, cand_party
) CL ON T.area_cd = CL.area_cd AND T.cluster = CL.cluster AND T.cand_party = CL.cand_party
LEFT JOIN (
SELECT AC2.area_cd, AC2.cand_party, AC2.cand_party_cd, AC2.area_cand_cnt, AC2.max_area_cand_cnt,
SUM(CASE WHEN AC2.area_cand_cnt <![CDATA[<]]> AC2.max_area_cand_cnt THEN AC2.area_cand_cnt ELSE 0 END) OVER(PARTITION BY AC2.area_cd, AC2.cand_party) AS loser_area_total_cnt
FROM (
SELECT AC.area_cd, AC.cand_party, AC.cand_party_cd, AC.area_cand_cnt,
MAX(AC.area_cand_cnt) OVER(PARTITION BY AC.area_cd, AC.cand_party) AS max_area_cand_cnt
FROM (
SELECT area_cd, cand_party, cand_party_cd, sum(cnt) AS area_cand_cnt
FROM (
SELECT area_cd, cluster, cand_party, cand_party_cd, sum(cnt) AS cnt
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
GROUP BY area_cd, cluster, cand_party, cand_party_cd
) X
GROUP BY area_cd, cand_party, cand_party_cd
) AC
) AC2
) AC3 ON T.area_cd = AC3.area_cd AND T.cand_party = AC3.cand_party AND T.cand_party_cd = AC3.cand_party_cd
LEFT JOIN (
SELECT option_cd,
CASE
WHEN TRY_CAST(weight_rate AS DECIMAL(18,10)) > 1 THEN TRY_CAST(weight_rate AS DECIMAL(18,10)) / 100.0
ELSE TRY_CAST(weight_rate AS DECIMAL(18,10))
END AS weight
FROM exitpoll.dbo.F_option
WHERE vote_cd = 2
AND option_type = 6
) W ON 1=1
) DO
<if test="f_local == 'CHOICE'">
WHERE area_cd = #{area_cd}
</if>
-- @@@@@ 15. TMP_OUTPUT_PRE 초기화
DELETE FROM exitpoll.dbo.TMP_OUTPUT_PRE
WHERE cand_party = #{f_cand_party}
<if test="f_local == 'CHOICE'">
AND area_cd = #{area_cd}
</if>
-- @@@@@ 14. TMP_PRE_P5N_CNT을 사용하여 층 X 후보(정당) X 사전 조사 옵션 별 count를 계산하여 TMP_OUTPUT_PRE에 넣는다
INSERT INTO exitpoll.dbo.TMP_OUTPUT_PRE
-- 선거구 - 후보별 사전투표율 옵션 1~9
/*
01 전화조사 판별(선관위 성by연령 반영)_DCO
02 전화조사 판별(전체)_DCO
03 전화조사 성by연령 repercent(선관위 성by연령 반영)_DCO
04 출구조사 0% (출구조사 사용, 선관위 성by연령 구성비)_이전과 동일
05 출구조사 1% (출구조사 사용, 특정후보 1% 추가, 나머지 타후보 비율배분)
06 출구조사 2% (출구조사 사용, 특정후보 2% 추가, 나머지 타후보 비율배분)
07 출구조사 3% (출구조사 사용, 특정후보 3% 추가, 나머지 타후보 비율배분)
08 출구조사 5% (출구조사 사용, 특정후보 5% 추가, 나머지 타후보 비율배분)
09 출구조사 7% (출구조사 사용, 특정후보 7% 추가, 나머지 타후보 비율배분)
10 출구조사 9% (출구조사 사용, 특정후보 9% 추가, 나머지 타후보 비율배분)
11 출구조사 11% (출구조사 사용, 특정후보 11% 추가, 나머지 타후보 비율배분)
12 출구조사 13% (출구조사 사용, 특정후보 13% 추가, 나머지 타후보 비율배분)
*/
SELECT area_cd, cand_party, cand_party_cd, option_cd, vote_rate
FROM (
SELECT area_cd, cand_party, option_cd, cand_party_cd, vote_rate FROM exitpoll.dbo.F_PRE WHERE cand_party = #{f_cand_party} UNION ALL
SELECT TPP.area_cd, cand_party, option_cd, TPP.cand_party_cd,
CASE SUM(pre_cnt) WHEN 0 THEN 0.0
ELSE SUM(pre_cnt) / SUM(pre_sum) * 100 -- 해당 후보의 현재 사전투표율(%)
+ CASE WHEN TPC.sel_pre_cnt IS NULL THEN weight_rate -- 특정 후보에게 가중치(weight_rate) 추가
-- 특정 후보에게 가중치(weight_rate)를 더한만큼 나머지 후보들은 그 합계만큼 비례해서 줄임
ELSE -1 * SUM(pre_cnt) / (SUM(pre_sum) - TPC.sel_pre_cnt) * weight_rate
END
END AS vote_rate
FROM exitpoll.dbo.TMP_PRE_P5N_CNT TPP LEFT JOIN (
SELECT FPC.area_cd, FPC.cand_party_cd, SUM(pre_cnt) AS sel_pre_cnt
FROM exitpoll.dbo.F_PRE_CAND FPC JOIN exitpoll.dbo.TMP_PRE_P5N_CNT TPPC
ON FPC.area_cd = TPPC.area_cd AND FPC.cand_party = #{f_cand_party} AND FPC.cand_party_cd = TPPC.cand_party_cd
GROUP BY FPC.area_cd, FPC.cand_party_cd
) TPC
ON TPP.area_cd = TPC.area_cd AND TPP.cand_party_cd != TPC.cand_party_cd LEFT JOIN
(
SELECT weight_rate, option_cd
FROM exitpoll.dbo.F_option
WHERE vote_cd = '1'
and option_type = '2'
--SELECT number + CASE WHEN number > 3 THEN number - 3 ELSE 0 END AS weight_rate, FORMAT(number+4, '00') AS option_cd
--FROM master..spt_values
--WHERE type = 'P' and 9 > number
) OPT ON 1=1
GROUP BY TPP.area_cd, cand_party, option_cd, weight_rate, TPP.cand_party_cd, TPC.sel_pre_cnt
) T
<if test="f_local == 'CHOICE'">
WHERE area_cd = #{area_cd}
</if>
-- @@@@@ 16. F_output 초기화
-- 최종 예측치
DELETE FROM exitpoll.dbo.F_output
WHERE cand_party = #{f_cand_party}
<if test="f_local == 'CHOICE'">
AND area_cd = #{area_cd}
</if>
-- @@@@@ 17. 층 X 후보(정당) X 옵션 별로 사전조사 득표율 * 사전조사 웨이팅, 당일조사 득표율 * 당일조사 웨이팅을 합하여 F_output에 넣는다
-- poll_pre 사전 조사 웨이팅 , poll_5p 5시이전 웨이팅 , poll_5n 5시 이후 웨이팅(0)
INSERT INTO exitpoll.dbo.F_output
SELECT DD.area_cd, DD.cand_party, PR.option_cd AS pre_option_cd, DD.option_cd, DD.cand_party_cd,
ISNULL(SUM(
W.cluster_weight * (
-- 사전투표 반영분 : 사전투표 결과 비율 * 사전투표 가중치
PR.vote_rate * W.poll_pre +
-- 당일투표 반영부(+5n 반영분)
-- time_band == 8 : 8차 18:00
<if test="time_band == 8">
DD.vote_rate * (CONVERT(NUMERIC(12, 10), W.poll_5p) + CONVERT(NUMERIC(12, 10), W.poll_5n))
</if>
<if test="time_band != 8">
DD.vote_rate * W.poll_5p +
PN.vote_rate * W.poll_5n
</if>
)
), 0) AS vote_rate
FROM
(
SELECT area_cd, cluster, cand_party, cand_party_cd, option_cd, CASE sum(cnt) WHEN 0 THEN 0.0 ELSE sum(cnt) / sum(sum_cnt) * 100 END AS vote_rate
FROM exitpoll.dbo.TMP_OUTPUT_DDAY
WHERE cand_party = #{f_cand_party}
GROUP BY area_cd, cluster, cand_party, cand_party_cd, option_cd
) DD LEFT JOIN exitpoll.dbo.TMP_OUTPUT_PRE PR
ON DD.area_cd = PR.area_cd AND DD.cand_party = PR.cand_party AND DD.cand_party_cd = PR.cand_party_cd LEFT JOIN
(
SELECT area_cd, cand_party, cand_party_cd,
CASE SUM(p5n_cnt) WHEN 0 THEN 0.0 ELSE SUM(p5n_cnt) / SUM(p5n_sum) * 100 END AS vote_rate
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
GROUP BY area_cd, cand_party, cand_party_cd
) PN ON DD.area_cd = PN.area_cd AND DD.cand_party = PN.cand_party AND DD.cand_party_cd = PN.cand_party_cd
LEFT JOIN exitpoll.dbo.F_weight W ON DD.area_cd = W.area_cd AND DD.cluster = W.cluster
<if test="f_local == 'CHOICE'">
WHERE DD.area_cd = #{area_cd}
</if>
GROUP BY DD.area_cd, DD.cand_party, PR.option_cd, DD.option_cd, DD.cand_party_cd
union all
<!-- 비례정당을 위한 사전 옵션 및 가중치 적용 안된 데이터들 - 박준성 -->
select DD.area_cd, DD.cand_party, '00' AS pre_option_cd, DD.option_cd, DD.cand_party_cd,
ISNULL(SUM(
cluster_weight * (
<if test="time_band == 8">
DD.vote_rate * (CONVERT(NUMERIC(12, 10), new_5p) + CONVERT(NUMERIC(12, 10), new_5n))
</if>
<if test="time_band != 8">
DD.vote_rate * new_5p +
PN.vote_rate * new_5n
</if>
)
), 0) AS vote_rate
from
(
SELECT area_cd, cluster, cand_party, cand_party_cd, option_cd, CASE sum(cnt) WHEN 0 THEN 0.0 ELSE sum(cnt) / sum(sum_cnt) * 100 END AS vote_rate
FROM exitpoll.dbo.TMP_OUTPUT_DDAY
WHERE cand_party = #{f_cand_party}
GROUP BY area_cd, cluster, cand_party, cand_party_cd, option_cd
) DD LEFT JOIN
(
SELECT area_cd, cand_party, cand_party_cd,
CASE SUM(p5n_cnt) WHEN 0 THEN 0.0 ELSE SUM(p5n_cnt) / SUM(p5n_sum) * 100 END AS vote_rate
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
GROUP BY area_cd, cand_party, cand_party_cd
) PN ON DD.area_cd = PN.area_cd AND DD.cand_party = PN.cand_party AND DD.cand_party_cd = PN.cand_party_cd
LEFT JOIN exitpoll.dbo.F_weight W ON DD.area_cd = W.area_cd AND DD.cluster = W.cluster
<if test="f_local == 'CHOICE'">
WHERE DD.area_cd = #{area_cd}
</if>
GROUP BY DD.area_cd, DD.cand_party, DD.option_cd, DD.cand_party_cd
<!-- 비례정당을 위한 사전 옵션 및 가중치 적용 안된 데이터들 - 박준성 -->
<if test="f_cand_party == 'CAND'">
union all
SELECT area_cd, 'CAND', option_cd, '00',cand_party_cd,vote_rate
FROM exitpoll.dbo.TMP_OUTPUT_PRE
WHERE cand_party = 'CAND'
<if test="f_local == 'CHOICE'">
and area_cd = #{area_cd}
</if>
</if>
DELETE exitpoll.dbo.F_output_time WHERE cand_party = #{f_cand_party} and time_band = #{time_band}
<if test="f_local == 'CHOICE'">
and area_cd = #{area_cd}
</if>
INSERT INTO exitpoll.dbo.F_output_time
SELECT #{time_band} as time_band, area_cd, cand_party, pre_option_cd, option_cd, cand_party_cd,vote_rate
FROM exitpoll.dbo.F_output
WHERE cand_party = #{f_cand_party}
<if test="f_local == 'CHOICE'">
and area_cd = #{area_cd}
</if>
SET NOCOUNT OFF
<!-- 보고서 1 투표구별 자료 -->
SELECT TEP.area_cd, cluster, cluster_nm, TEP.subarea_cd, subarea_nm,
CASE TEP.cand_party_cd WHEN '00' THEN '999' ELSE TEP.cand_party_cd END as cand_party_cd,
<if test="f_cand_party == 'CAND'"> ISNULL(CASE WHEN C.quit = '1' then C.cand_nm + ' (사퇴)' else C.cand_nm end, '무응답') AS cand_nm, </if>
<if test="f_cand_party == 'PARTY'"> ISNULL(p.party_nm, '무응답') AS cand_nm, </if>
<if test="f_cand_party == 'EDU'"> ISNULL(E.edu_nm, '무응답') AS cand_nm, </if>
ISNULL(P.party_nm, '무응답') AS party_nm, SUM(cnt) AS cnt
FROM
exitpoll.dbo.TMP_EXIT_POLL TEP LEFT JOIN exitpoll.dbo.F_subarea SA
ON TEP.area_cd = SA.area_cd AND TEP.subarea_cd = SA.subarea_cd
<if test="f_cand_party == 'CAND'">
LEFT JOIN exitpoll.dbo.F_cand C ON TEP.area_cd = C.area_cd AND TEP.cand_party_cd = C.cand_cd
</if>
<if test="f_cand_party == 'EDU'">
LEFT JOIN exitpoll.dbo.F_Edu E ON TEP.area_cd = E.area_cd AND TEP.cand_party_cd = E.edu_cd AND E.quit = '0'
</if>
LEFT JOIN exitpoll.dbo.F_party P ON
<if test="f_cand_party == 'CAND'"> C.party_cd </if>
<if test="f_cand_party == 'PARTY'"> TEP.cand_party_cd </if>
<if test="f_cand_party == 'EDU'"> '99' </if>
= P.party_cd
WHERE (TEP.cand_party_cd = '00' OR P.party_nm IS NOT NULL)
GROUP BY TEP.area_cd, cluster, cluster_nm, TEP.subarea_cd, subarea_nm,
TEP.cand_party_cd, P.party_nm
<if test="f_cand_party == 'CAND'"> ,C.cand_nm, C.quit </if>
<if test="f_cand_party == 'EDU'"> ,E.edu_nm </if>
ORDER BY TEP.area_cd, SA.cluster, TEP.subarea_cd, CASE TEP.cand_party_cd WHEN '00' THEN '999' ELSE TEP.cand_party_cd END
<!-- 보고서 1 -->
<!-- 보고서 2 -->
SELECT area_cd, cluster, cluster_nm, sect, cand_party_cd, ROUND(sum(sum_cnt), 0) AS sum_cnt,
CASE WHEN sum(sum_cnt) = 0 THEN 0 ELSE ROUND(sum(cnt) / sum(sum_cnt) * 100.00, 1) END AS rate
FROM
(
SELECT area_cd, cluster, cluster_nm,
CASE sect
WHEN '10_남자' THEN '001_남자'
WHEN '20_여자' THEN '002_여자'
ELSE sect END as sect,
CASE cand_party_cd WHEN '00' THEN '999' ELSE cand_party_cd END as cand_party_cd,
sum_cnt, cnt
FROM exitpoll.dbo.TMP_CLUSTER_DATA
) T
GROUP BY area_cd, cluster, cluster_nm, sect, cand_party_cd
ORDER BY area_cd, cluster, sect, cand_party_cd
<!-- 보고서 2 -->
<!-- 보고서 3 -->
SELECT P5P.area_cd, P5P.cand_party_cd, p5p_rate, pre_rate, p5n_rate
FROM
(
SELECT area_cd, cand_party_cd, ROUND(CASE sum(cnt) WHEN 0 THEN 0.0 ELSE sum(cnt) / sum(sum_cnt) * 100 END, 1) AS p5p_rate,
ROUND(CASE SUM(p5n_cnt) WHEN 0 THEN 0.0 ELSE SUM(p5n_cnt) / SUM(p5n_sum) * 100 END, 1) AS p5n_rate
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
GROUP BY area_cd, cand_party_cd
UNION ALL
SELECT area_cd, '999', ROUND(CASE sum(n_cnt) WHEN 0 THEN 0.0 ELSE sum(n_cnt) / sum(sum_cnt) * 100 END, 1), NULL
FROM exitpoll.dbo.TMP_PRE_P5N_CNT
GROUP BY area_cd
) P5P LEFT JOIN (
SELECT area_cd, cand_party_cd,
JSON_QUERY('[' + STRING_AGG('{"option_cd":"' + option_cd + '","vote_rate":"' + FORMAT(ROUND(vote_rate, 1), '#0.0') + '"}', ',') + ']') AS pre_rate
FROM exitpoll.dbo.TMP_OUTPUT_PRE
WHERE cand_party = #{f_cand_party}
GROUP BY area_cd, cand_party_cd
) PRE ON P5P.area_cd = PRE.area_cd AND P5P.cand_party_cd = PRE.cand_party_cd
ORDER BY P5P.area_cd, P5P.cand_party_cd
<!-- 보고서 3-->
<!-- 보고서 4 -->
<!-- 최종 선택된 옵션과 옵션 기준 득표율 (chk = '1' 선택, chk = '0' 미선택) -->
SELECT OP.area_cd, OP.cand_party, OP.option_cd, OP.cand_party_cd,
ROUND(vote_rate, 1) as vote_rate, CASE WHEN OO.area_cd IS NULL THEN '0' ELSE '1' END AS chk
FROM exitpoll.dbo.F_output OP
LEFT JOIN exitpoll.dbo.F_output_option OO
ON OP.area_cd = OO.area_cd AND OO.vote_cd = '2' AND OP.option_cd =
<if test="f_cand_party == 'CAND'"> OO.cand_option_cd </if>
<if test="f_cand_party == 'PARTY'"> OO.party_option_cd </if>
<if test="f_cand_party == 'EDU'"> OO.edu_option_cd </if>
JOIN exitpoll.dbo.F_output_option POO
ON OP.area_cd = POO.area_cd AND POO.vote_cd = '1' AND OP.pre_option_cd =
<if test="f_cand_party == 'CAND'"> POO.cand_option_cd </if>
<if test="f_cand_party == 'PARTY'"> POO.party_option_cd </if>
<if test="f_cand_party == 'EDU'"> POO.edu_option_cd </if>
WHERE cand_party = #{f_cand_party}
ORDER BY OP.area_cd, OP.option_cd, OP.cand_party_cd
<!-- 보고서 4 -->
<!-- 당일 옵션 ++++ 보고서 3 뒤에 붙이기 -->
SELECT area_cd, cand_party, cand_party_cd, option_cd, FORMAT(SUM(cls_rate * cluster_weight), '#0.0') AS vote_rate
FROM
(
SELECT DD.area_cd, DD.cand_party, DD.cand_party_cd, DD.option_cd, WE.cluster, WE.cluster_weight, CASE SUM(cnt) WHEN 0 THEN 0.0 ELSE SUM(cnt) / SUM(sum_cnt) * 100 END AS cls_rate
FROM exitpoll.dbo.TMP_OUTPUT_DDAY DD LEFT JOIN exitpoll.dbo.F_Weight WE ON DD.area_cd = WE.area_cd AND DD.cluster = WE.cluster
WHERE DD.cand_party = #{f_cand_party}
GROUP BY DD.area_cd, DD.cand_party, DD.cand_party_cd, DD.option_cd, WE.cluster, WE.cluster_weight
) T
GROUP BY area_cd, cand_party, cand_party_cd, option_cd
<!-- 당일 옵션 -->
<!-- 보고서 5 -->
SELECT time_band
,OT.area_cd
,OT.cand_party
,OT.pre_option_cd
,OT.option_cd
,OT.cand_party_cd
,OT.vote_rate
FROM exitpoll.dbo.F_output_time OT
JOIN exitpoll.dbo.F_output_option OO ON OT.area_cd = OO.area_cd AND OO.vote_cd = '2' AND OT.option_cd =
<if test="f_cand_party == 'CAND'"> OO.cand_option_cd </if>
<if test="f_cand_party == 'PARTY'"> OO.party_option_cd </if>
<if test="f_cand_party == 'EDU'"> OO.edu_option_cd </if>
JOIN exitpoll.dbo.F_output_option POO ON OT.area_cd = POO.area_cd AND POO.vote_cd = '1' AND OT.pre_option_cd =
<if test="f_cand_party == 'CAND'"> POO.cand_option_cd </if>
<if test="f_cand_party == 'PARTY'"> POO.party_option_cd </if>
<if test="f_cand_party == 'EDU'"> POO.edu_option_cd </if>
where OT.cand_party = #{f_cand_party}
ORDER BY OT.area_cd, OT.option_cd, OT.cand_party_cd
<!-- 보고서 5 -->
</select>
<select id="getLastReport" resultMap="AnalysisVo">
<!-- 보고서 3 사전투표 옵션 -->
<!-- 최종 선택된 옵션과 옵션 기준 득표율 (chk = '1' 선택, chk = '0' 미선택) -->
SELECT OP.area_cd, OP.cand_party, OP.option_cd, OP.cand_party_cd,
ROUND(vote_rate, 1) as vote_rate, CASE WHEN OO.area_cd IS NULL THEN '0' ELSE '1' END AS chk
FROM exitpoll.dbo.F_output OP
LEFT JOIN exitpoll.dbo.F_output_option OO
ON OP.area_cd = OO.area_cd AND OO.vote_cd = '2' AND OP.option_cd =
<if test="f_cand_party == 'CAND'"> OO.cand_option_cd </if>
<if test="f_cand_party == 'PARTY'"> OO.party_option_cd </if>
<if test="f_cand_party == 'EDU'"> OO.edu_option_cd </if>
JOIN exitpoll.dbo.F_output_option POO
ON OP.area_cd = POO.area_cd AND POO.vote_cd = '1' AND OP.pre_option_cd =
<if test="f_cand_party == 'CAND'"> POO.cand_option_cd </if>
<if test="f_cand_party == 'PARTY'"> POO.party_option_cd </if>
<if test="f_cand_party == 'EDU'"> POO.edu_option_cd </if>
WHERE cand_party = #{f_cand_party}
ORDER BY OP.area_cd, OP.option_cd, OP.cand_party_cd
<!-- 보고서 4 -->
</select>
<select id="getTimeReport" resultMap="AnalysisVo">
<!-- 보고서 5 -->
SELECT time_band
,OT.area_cd
,OT.cand_party
,OT.pre_option_cd
,OT.option_cd
,OT.cand_party_cd
,OT.vote_rate
FROM exitpoll.dbo.F_output_time OT
JOIN exitpoll.dbo.F_output_option OO ON OT.area_cd = OO.area_cd AND OO.vote_cd = '2' AND OT.option_cd =
<if test="f_cand_party == 'CAND'"> OO.cand_option_cd </if>
<if test="f_cand_party == 'PARTY'"> OO.party_option_cd </if>
<if test="f_cand_party == 'EDU'"> OO.edu_option_cd </if>
JOIN exitpoll.dbo.F_output_option POO ON OT.area_cd = POO.area_cd AND POO.vote_cd = '1' AND OT.pre_option_cd =
<if test="f_cand_party == 'CAND'"> POO.cand_option_cd </if>
<if test="f_cand_party == 'PARTY'"> POO.party_option_cd </if>
<if test="f_cand_party == 'EDU'"> POO.edu_option_cd </if>
where OT.cand_party = #{f_cand_party}
ORDER BY OT.area_cd, OT.option_cd, OT.cand_party_cd
<!-- 보고서 5 -->
</select>
<select id="getOutputFinalDataCnt" resultType="integer">
SELECT count(*) as cnt FROM exitpoll.dbo.F_output_final
WHERE CAND_PARTY = #{f_cand_party}
</select>
<insert id="putOutputFinalData">
INSERT INTO exitpoll.dbo.F_output_final
SELECT OP.area_cd, OP.cand_party, OP.pre_option_cd, OP.option_cd,
CASE OP.cand_party_cd WHEN '00' THEN '999' ELSE OP.cand_party_cd END as cand_party_cd,
vote_rate
FROM exitpoll.dbo.F_output OP
JOIN exitpoll.dbo.F_output_option OO
ON OP.area_cd = OO.area_cd AND OO.vote_cd = '2' AND OP.option_cd =
<if test="f_cand_party == 'CAND'"> OO.cand_option_cd </if>
<if test="f_cand_party == 'PARTY'"> OO.party_option_cd </if>
<if test="f_cand_party == 'EDU'"> OO.edu_option_cd </if>
JOIN exitpoll.dbo.F_output_option POO
ON OP.area_cd = POO.area_cd AND POO.vote_cd = '1' AND OP.pre_option_cd =
<if test="f_cand_party == 'CAND'"> POO.cand_option_cd </if>
<if test="f_cand_party == 'PARTY'"> '00' </if> <!-- 비례정당은 사전 옵션 사용 X -->
<if test="f_cand_party == 'EDU'"> POO.edu_option_cd </if>
WHERE cand_party = #{f_cand_party}
ORDER BY OP.area_cd, OP.option_cd, CASE OP.cand_party_cd WHEN '00' THEN '999' ELSE OP.cand_party_cd END
</insert>
<select id="getOutputFinalData" resultMap="AnalysisVo">
<if test="f_cand_party == 'PARTY'">
SELECT B.*, A.PARTY_NM FROM
(
SELECT PARTY_CD, PARTY_NM FROM exitpoll.dbo.F_PARTY WHERE PARTY_YN = '1'
) A
LEFT JOIN
(
</if>
SELECT OPF.area_cd, AR.area_nm, OPF.cand_party, OPF.pre_option_cd
<if test="f_cand_party == 'PARTY'"> <!-- 비례정당은 사전 옵션 사용 X -->
, 'X' AS pre_option_nm
</if>
<if test="f_cand_party != 'PARTY'"> <!-- 비례정당은 사전 옵션 사용 X -->
, O1.option_nm AS pre_option_nm
</if>
, OPF.option_cd, O2.option_nm, OPF.cand_party_cd, OPF.vote_rate
FROM exitpoll.dbo.F_output_final OPF
INNER JOIN exitpoll.dbo.F_area AR
ON OPF.area_cd = AR.area_cd
<if test="f_cand_party != 'PARTY'"> <!-- 비례정당은 사전 옵션 사용 X -->
INNER JOIN exitpoll.dbo.F_option O1
ON OPF.pre_option_cd = O1.option_cd AND O1.vote_cd = '1'
</if>
INNER JOIN exitpoll.dbo.F_option O2
ON OPF.option_cd = O2.option_cd AND O2.vote_cd = '2'
WHERE OPF.CAND_PARTY = #{f_cand_party}
<if test="f_cand_party == 'PARTY'">
) B ON A.PARTY_CD = B.CAND_PARTY_CD
</if>
</select>
<update id="editOutputFinalData">
UPDATE exitpoll.dbo.F_output_final
SET vote_rate = #{vote_rate}
WHERE AREA_CD = #{area_cd}
AND cand_party = #{f_cand_party}
AND cand_party_cd = #{cand_party_cd}
</update>
<delete id="dataInitialization">
DELETE exitpoll.dbo.F_output_final
WHERE cand_party = #{f_cand_party}
</delete>
<select id="optionCompare" resultMap="AnalysisVo">
SELECT OPF.area_cd, OO.vote_cd, CASE WHEN OO.vote_cd = '1' THEN OPF.pre_option_cd ELSE OPF.option_cd END option_cd,
<if test="f_cand_party == 'CAND'"> OO.cand_option_cd as final_option_cd </if>
<if test="f_cand_party == 'PARTY'"> OO.party_option_cd as final_option_cd </if>
<if test="f_cand_party == 'EDU'"> OO.edu_option_cd as final_option_cd </if>
FROM exitpoll.dbo.F_output_final OPF
INNER JOIN exitpoll.dbo.F_output_option OO
ON OPF.area_cd = OO.area_cd
WHERE OPF.cand_party = #{f_cand_party}
GROUP BY OPF.AREA_CD, OO.vote_cd, OPF.pre_option_cd, OPF.option_cd,
<if test="f_cand_party == 'CAND'"> OO.CAND_OPTION_CD </if>
<if test="f_cand_party == 'PARTY'"> OO.party_option_cd </if>
<if test="f_cand_party == 'EDU'"> OO.edu_option_cd </if>
</select>
<update id="editOptionCommitFinalData">
UPDATE exitpoll.dbo.F_output_final
SET
<if test="vote_cd == 1">
pre_option_cd = B.pre_option_cd,
</if>
<if test="vote_cd == 2">
option_cd = B.option_cd,
</if>
vote_rate = B.vote_rate
FROM exitpoll.dbo.F_output_final A
LEFT JOIN exitpoll.dbo.F_output B
ON A.area_cd = B.area_cd
AND A.cand_party = B.cand_party
AND A.cand_party_cd = B.cand_party_cd
WHERE A.area_cd = #{area_cd}
AND A.cand_party = #{f_cand_party}
<if test="vote_cd == 1">
AND B.pre_option_cd = #{option_cd}
</if>
<if test="vote_cd == 2">
AND B.option_cd = #{option_cd}
</if>
</update>
<update id="editOptionRollback">
UPDATE exitpoll.dbo.F_output_option
<if test="f_cand_party == 'CAND'"> SET cand_option_cd = #{option_cd} </if>
<if test="f_cand_party == 'PARTY'"> SET party_option_cd = #{option_cd} </if>
<if test="f_cand_party == 'EDU'"> SET edu_option_cd = #{option_cd} </if>
WHERE area_cd = #{area_cd}
AND vote_cd = #{vote_cd}
</update>
<insert id="insertHtml">
INSERT INTO exitpoll.dbo.F_HTML(search_req, html_str, insert_user) VALUES (#{search_req}, #{html_str}, #{insert_user});
</insert>
<select id="historyDate" resultMap="AnalysisVo">
SELECT id, search_req, CONVERT(varchar(19), insert_date, 120) as insert_date
FROM exitpoll.dbo.F_HTML
WHERE flag = '1'
ORDER BY insert_date DESC
</select>
<select id="historyHtml" resultMap="AnalysisVo">
SELECT html_str FROM exitpoll.dbo.F_HTML WHERE id = #{id}
</select>
<update id="deleteHistory">
UPDATE exitpoll.dbo.F_HTML
SET flag = '0', update_date = getdate()
WHERE id = #{id}
</update>
</mapper>