사전투표(출1~출8), 열세자 옵션 하드코딩 -> F_option 조회로 변경

This commit is contained in:
2026-04-14 11:25:04 +09:00
parent 61a337ca89
commit db2a45efc0

View File

@@ -27,9 +27,9 @@
</select> </select>
<select id="getOption" resultMap="AnalysisVo1,AnalysisVo2"> <select id="getOption" resultMap="AnalysisVo1,AnalysisVo2">
SELECT vote_cd, option_cd, option_nm 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_2022le.dbo.F_option FROM exitpoll_2022le.dbo.F_option
ORDER BY vote_cd, option_cd 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 SELECT area_cd, vote_cd, cand_option_cd, party_option_cd, edu_option_cd
FROM exitpoll_2022le.dbo.F_output_option FROM exitpoll_2022le.dbo.F_output_option
@@ -64,17 +64,18 @@
DROP TABLE IF EXISTS exitpoll_2022le.dbo.TMP_CLUSTER_DATA DROP TABLE IF EXISTS exitpoll_2022le.dbo.TMP_CLUSTER_DATA
DROP TABLE IF EXISTS exitpoll_2022le.dbo.TMP_WEIGHT_GA DROP TABLE IF EXISTS exitpoll_2022le.dbo.TMP_WEIGHT_GA
DROP TABLE IF EXISTS exitpoll_2022le.dbo.TMP_PRE_P5N_CNT DROP TABLE IF EXISTS exitpoll_2022le.dbo.TMP_PRE_P5N_CNT
DROP TABLE IF EXISTS exitpoll_2022le.dbo.TMP_PRE_P5N_RATIO;
-- @@@@@ 2. F_EXITPOLL에서 선택된 차수 보다 작거나 같은 Data를 F_TMP_EXPOLL에 넣는다 -- @@@@@ 2. F_EXITPOLL에서 선택된 차수 보다 작거나 같은 Data를 F_TMP_EXPOLL에 넣는다
SELECT a.* INTO exitpoll_2022le.dbo.F_TMP_EXPOLL SELECT a.* INTO exitpoll_2022le.dbo.F_TMP_EXPOLL
FROM exitpoll_2022le.dbo.F_EXITPOLL a FROM exitpoll_2022le.dbo.F_EXITPOLL a
left join exitpoll_2022le.dbo.F_cand c on a.area_cd=c.area_cd and a.cand_cd=c.cand_cd and c.quit = 0 left join exitpoll_2022le.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 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 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' --> <!-- and reg_date <![CDATA[<=]]> '2024-04-10 16:54:59.999' -->
DROP TABLE IF EXISTS exitpoll_2022le.dbo.TMP_PRE_P5N_RATIO;
-- @@@@@ F_TMP_EXPOLL 기준으로 지역 X 투표소 X 성 X 연령 X 후보(정당)의 count값을 임시 테이블 TMP_EXIT_POLL에 넣는다 -- @@@@@ F_TMP_EXPOLL 기준으로 지역 X 투표소 X 성 X 연령 X 후보(정당)의 count값을 임시 테이블 TMP_EXIT_POLL에 넣는다
SELECT B.area_cd, B.subarea_cd, SELECT B.area_cd, B.subarea_cd,
B.gender, B.age, cand_party, B.gender, B.age, cand_party,
@@ -400,8 +401,8 @@
-- 성연령별 가중치 적용 (사전, 5시 이후) -- 성연령별 가중치 적용 (사전, 5시 이후)
SELECT A.area_cd, A.cluster, A.cand_party, A.cand_party_cd, A.sect, A.sect_str, sum_cnt, n_cnt, cnt, 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) * pre_ratio AS pre_sum, cnt * pre_ratio AS pre_cnt, (sum_cnt-n_cnt) * B.pre_ratio AS pre_sum, cnt * B.pre_ratio AS pre_cnt,
(sum_cnt-n_cnt) * p5n_ratio AS p5n_sum, cnt * p5n_ratio AS p5n_cnt (sum_cnt-n_cnt) * B.p5n_ratio AS p5n_sum, cnt * B.p5n_ratio AS p5n_cnt
INTO exitpoll_2022le.dbo.TMP_PRE_P5N_CNT INTO exitpoll_2022le.dbo.TMP_PRE_P5N_CNT
FROM FROM
( (
@@ -453,13 +454,18 @@
-- 05.열세자 85%(성/연령) @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -- 05.열세자 85%(성/연령) @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-- 06.열세자 80%(성/연령) @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -- 06.열세자 80%(성/연령) @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SELECT T.area_cd, T.cluster, T.cand_party, T.sect, T.cand_party_cd, option_cd, sum_cnt, 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 cnt + CASE sum_cnt - n_cnt WHEN 0 THEN 0 ELSE -- 무응답이 없으니 배분하지 않음
CASE WHEN max_cnt IS NULL THEN CASE WHEN max_cnt IS NULL THEN -- 단독 1위가 없음( 모든 후보에게 현재 득표 비율대로 n_cnt를 비례배분)
cnt / (sum_cnt - n_cnt) * n_cnt cnt / (sum_cnt - n_cnt) * n_cnt
WHEN max_cnt = cnt THEN WHEN max_cnt = cnt THEN -- 단독 1위가 있음( 1위 후보의 n_cnt 목에서 깍음) ex)열세자 95% 인경우 받을게 10이면 9.5만 받음
cnt / (sum_cnt - n_cnt) * n_cnt * weight cnt / (sum_cnt - n_cnt) * n_cnt * weight
ELSE ELSE -- 1위가 아닌 후보일 때
CASE WHEN (sum_cnt - n_cnt) - max_cnt = 0 THEN (n_cnt - (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 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
END AS cnt END AS cnt
FROM exitpoll_2022le.dbo.TMP_PRE_P5N_CNT T LEFT JOIN ( FROM exitpoll_2022le.dbo.TMP_PRE_P5N_CNT T LEFT JOIN (
@@ -472,14 +478,10 @@
GROUP BY area_cd, cluster, cand_party, sect, cnt HAVING sum(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 ) 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 0.95 AS weight, '03' AS option_cd UNION ALL select weight_rate as weight, option_cd
SELECT 0.90 AS weight, '04' AS option_cd UNION ALL from exitpoll_2022le.dbo.F_option
SELECT 0.85 AS weight, '05' AS option_cd UNION ALL where vote_cd = 2
SELECT 0.80 AS weight, '06' AS option_cd UNION ALL and option_type = 4
SELECT 1.05 AS weight, '11' AS option_cd UNION ALL
SELECT 1.10 AS weight, '12' AS option_cd UNION ALL
SELECT 1.15 AS weight, '13' AS option_cd UNION ALL
SELECT 1.20 AS weight, '14' AS option_cd
) W ON 1=1 ) W ON 1=1
UNION ALL UNION ALL
@@ -511,14 +513,10 @@
GROUP BY area_cd, cluster, cand_party, cnt HAVING sum(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 ) MC ON T.area_cd = MC.area_cd AND T.cand_party = MC.cand_party AND T.cluster = MC.cluster LEFT JOIN
( (
SELECT 0.95 AS weight, '07' AS option_cd UNION ALL select weight_rate as weight, option_cd
SELECT 0.90 AS weight, '08' AS option_cd UNION ALL from exitpoll_2022le.dbo.F_option
SELECT 0.85 AS weight, '09' AS option_cd UNION ALL where vote_cd = 2
SELECT 0.80 AS weight, '10' AS option_cd UNION ALL and option_type = 5
SELECT 1.05 AS weight, '15' AS option_cd UNION ALL
SELECT 1.10 AS weight, '16' AS option_cd UNION ALL
SELECT 1.15 AS weight, '17' AS option_cd UNION ALL
SELECT 1.20 AS weight, '18' AS option_cd
) W ON 1=1 ) W ON 1=1
) DO ) DO
<if test="f_local == 'CHOICE'"> <if test="f_local == 'CHOICE'">
@@ -552,9 +550,14 @@
SELECT area_cd, cand_party, cand_party_cd, option_cd, vote_rate SELECT area_cd, cand_party, cand_party_cd, option_cd, vote_rate
FROM ( FROM (
SELECT area_cd, cand_party, option_cd, cand_party_cd, vote_rate FROM exitpoll_2022le.dbo.F_PRE WHERE cand_party = #{f_cand_party} UNION ALL SELECT area_cd, cand_party, option_cd, cand_party_cd, vote_rate FROM exitpoll_2022le.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 SELECT TPP.area_cd, cand_party, option_cd, TPP.cand_party_cd,
SUM(pre_cnt) / SUM(pre_sum) * 100 CASE SUM(pre_cnt) WHEN 0 THEN 0.0
+ CASE WHEN TPC.sel_pre_cnt IS NULL THEN weight_rate ELSE -1 * SUM(pre_cnt) / (SUM(pre_sum) - TPC.sel_pre_cnt) * weight_rate END END AS vote_rate 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_2022le.dbo.TMP_PRE_P5N_CNT TPP LEFT JOIN ( FROM exitpoll_2022le.dbo.TMP_PRE_P5N_CNT TPP LEFT JOIN (
SELECT FPC.area_cd, FPC.cand_party_cd, SUM(pre_cnt) AS sel_pre_cnt SELECT FPC.area_cd, FPC.cand_party_cd, SUM(pre_cnt) AS sel_pre_cnt
FROM exitpoll_2022le.dbo.F_PRE_CAND FPC JOIN exitpoll_2022le.dbo.TMP_PRE_P5N_CNT TPPC FROM exitpoll_2022le.dbo.F_PRE_CAND FPC JOIN exitpoll_2022le.dbo.TMP_PRE_P5N_CNT TPPC
@@ -563,9 +566,13 @@
) TPC ) TPC
ON TPP.area_cd = TPC.area_cd AND TPP.cand_party_cd != TPC.cand_party_cd LEFT JOIN ON TPP.area_cd = TPC.area_cd AND TPP.cand_party_cd != TPC.cand_party_cd LEFT JOIN
( (
SELECT number + CASE WHEN number > 3 THEN number - 3 ELSE 0 END AS weight_rate, FORMAT(number+4, '00') AS option_cd SELECT weight_rate, option_cd
FROM master..spt_values FROM exitpoll_2022le.dbo.F_option
WHERE type = 'P' and 9 > number 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 ) OPT ON 1=1
GROUP BY TPP.area_cd, cand_party, option_cd, weight_rate, TPP.cand_party_cd, TPC.sel_pre_cnt GROUP BY TPP.area_cd, cand_party, option_cd, weight_rate, TPP.cand_party_cd, TPC.sel_pre_cnt
) T ) T
@@ -586,14 +593,17 @@
INSERT INTO exitpoll_2022le.dbo.F_output INSERT INTO exitpoll_2022le.dbo.F_output
SELECT DD.area_cd, DD.cand_party, PR.option_cd AS pre_option_cd, DD.option_cd, DD.cand_party_cd, SELECT DD.area_cd, DD.cand_party, PR.option_cd AS pre_option_cd, DD.option_cd, DD.cand_party_cd,
ISNULL(SUM( ISNULL(SUM(
cluster_weight * ( W.cluster_weight * (
PR.vote_rate * poll_pre + -- 사전투표 반영분 : 사전투표 결과 비율 * 사전투표 가중치
PR.vote_rate * W.poll_pre +
-- 당일투표 반영부(+5n 반영분)
-- time_band == 8 : 8차 18:00
<if test="time_band == 8"> <if test="time_band == 8">
DD.vote_rate * (CONVERT(NUMERIC(12, 10), poll_5p) + CONVERT(NUMERIC(12, 10), poll_5n)) DD.vote_rate * (CONVERT(NUMERIC(12, 10), W.poll_5p) + CONVERT(NUMERIC(12, 10), W.poll_5n))
</if> </if>
<if test="time_band != 8"> <if test="time_band != 8">
DD.vote_rate * poll_5p + DD.vote_rate * W.poll_5p +
PN.vote_rate * poll_5n PN.vote_rate * W.poll_5n
</if> </if>
) )
), 0) AS vote_rate ), 0) AS vote_rate
@@ -799,7 +809,7 @@
</select> </select>
<select id="getLastReport" resultMap="AnalysisVo"> <select id="getLastReport" resultMap="AnalysisVo">
<!-- 보고서 4 --> <!-- 보고서 3 사전투표 옵션 -->
<!-- 최종 선택된 옵션과 옵션 기준 득표율 (chk = '1' 선택, chk = '0' 미선택) --> <!-- 최종 선택된 옵션과 옵션 기준 득표율 (chk = '1' 선택, chk = '0' 미선택) -->
SELECT OP.area_cd, OP.cand_party, OP.option_cd, OP.cand_party_cd, 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 ROUND(vote_rate, 1) as vote_rate, CASE WHEN OO.area_cd IS NULL THEN '0' ELSE '1' END AS chk