<% sql = "" sql = sql & " CREATE TABLE #statistics_next ( "&VbCrlf sql = sql & " hd_cd varchar(5) not null, "&VbCrlf sql = sql & " hd_name varchar(50) not null, "&VbCrlf sql = sql & " team_cd varchar(5) not null, "&VbCrlf sql = sql & " team_name varchar(50) not null, "&VbCrlf sql = sql & " p_sn varchar(10) not null, "&VbCrlf sql = sql & " p_child_sn varchar(15) not null, "&VbCrlf sql = sql & " p_depart varchar(5) not null, "&VbCrlf sql = sql & " p_statistics varchar(5) not null, "&VbCrlf sql = sql & " p_total_cost money not null, "&VbCrlf sql = sql & " p_cont_cost money not null, "&VbCrlf sql = sql & " est_money money not null, "&VbCrlf sql = sql & " act_money money not null, "&VbCrlf sql = sql & " cost_money money not null, "&VbCrlf sql = sql & " entertain_money money not null, "&VbCrlf sql = sql & " tf_money money not null, "&VbCrlf sql = sql & " benefit money not null, "&VbCrlf sql = sql & " p_progress_status varchar(6) not null, "&VbCrlf sql = sql & " complete_yn varchar(3) not null, "&VbCrlf sql = sql & " p_input_dt datetime not null, "&VbCrlf sql = sql & " p_con_date_end varchar(15) not null "&VbCrlf sql = sql & " , p_decide_date varchar(20) null "&VbCrlf sql = sql & " , weeknum int default '0' null "&VbCrlf sql = sql & " ) "&VbCrlf sql = sql & " "&VbCrlf sql = sql & " "&VbCrlf sql = sql & " INSERT INTO #statistics_next "&VbCrlf sql = sql & " SELECT TG.hd_cd, TG.hd_name, TG.team_cd, TG.team_name, "&VbCrlf sql = sql & " RT.p_sn, RT.p_child_sn, RT.p_depart, RT.p_statistics, "&VbCrlf sql = sql & " CASE WHEN (SELECT COUNT(*) FROM V_PROJECT_METHOD WITH(NOLOCK) WHERE p_sn = RT.p_sn AND p_statistics < RT.p_statistics) > 0 THEN 0 ELSE "&VbCrlf sql = sql & " CASE WHEN pb_depart <> p_depart_dep AND p_depart_dep <> '900' AND p_depart_dep <> '90' THEN 0 ELSE RT.p_total_cost END "&VbCrlf sql = sql & " END as p_total_cost, "&VbCrlf sql = sql & " CASE WHEN RT.p_progress_status < '008' THEN '0' ELSE RT.p_cont_cost END as p_cont_cost, "&VbCrlf sql = sql & " CASE WHEN RT.p_progress_status < '008' THEN '0' ELSE RT.est_money END as est_money, "&VbCrlf sql = sql & " CASE WHEN RT.p_progress_status < '008' THEN '0' ELSE RT.act_money END as act_money, "&VbCrlf sql = sql & " CASE WHEN RT.p_progress_status < '008' THEN '0' ELSE RT.cost_money END as cost_money, "&VbCrlf sql = sql & " CASE WHEN RT.p_progress_status < '008' THEN '0' ELSE RT.entertain_money END as entertain_money, "&VbCrlf sql = sql & " '0' as tf_money, "&VbCrlf sql = sql & " CASE WHEN RT.p_progress_status < '008' THEN '0' ELSE CASE WHEN complete_yn = '1' THEN RT.act_benefit ELSE RT.est_benefit END END as benefit, "&VbCrlf sql = sql & " RT.p_progress_status, RT.complete_yn, p_input_dt, p_con_date_end "&VbCrlf sql = sql & " , p_decide_date "&VbCrlf sql = sql & " , (SELECT weeknum FROM BG_WEEKNUM with(nolock) WHERE del_yn = 'N' and p_decide_date BETWEEN sdate AND edate) weeknum "&VbCrlf sql = sql & " FROM "&VbCrlf sql = sql & " ( "&VbCrlf sql = sql & " SELECT VPM.p_sn, VPM.p_child_sn, pb_depart, VPM.p_rsch_dept, MP.p_depart_dep, "&VbCrlf sql = sql & " CASE WHEN pb_depart = '12' THEN '12' "&VbCrlf sql = sql & " ELSE ISNULL(MP.p_depart_dep, ISNULL(VPM.p_rsch_dept, '')) END as p_depart, "&VbCrlf sql = sql & " ISNULL(p_statistics, '') as p_statistics, "&VbCrlf sql = sql & " CASE WHEN VPM.p_child_sn IS NULL THEN ISNULL(MP.p_budget, 0) ELSE CASE WHEN PDD.p_child_sn IS NULL THEN 0 ELSE ISNULL(MP.p_budget, 0) END END as p_total_cost, "&VbCrlf sql = sql & " ISNULL(VPM.p_cont_cost, MP.p_cont_cost) as p_cont_cost, "&VbCrlf sql = sql & " ISNULL(DC.est_money, 0) as est_money, "&VbCrlf sql = sql & " ISNULL(DC.act_money, 0) as act_money, "&VbCrlf sql = sql & " ISNULL(AC.cost_money, 0) as cost_money, "&VbCrlf sql = sql & " ISNULL(EM.entertain_money, 0) as entertain_money, "&VbCrlf sql = sql & " ISNULL(VPM.p_cont_cost, MP.p_cont_cost) - ISNULL(DC.est_money, 0) - round((ISNULL(VPM.p_cont_cost, MP.p_cont_cost) * 0.02), 0) as est_benefit, "&VbCrlf sql = sql & " ISNULL(VPM.p_cont_cost, MP.p_cont_cost) - ISNULL(DC.act_money, 0) - ISNULL(AC.cost_money, 0) - ISNULL(EM.entertain_money, 0) as act_benefit, "&VbCrlf sql = sql & " ISNULL(VPM.p_progress_status, '000') as p_progress_status, "&VbCrlf sql = sql & " ISNULL(p_research_complete*p_field_complete*p_dp_complete, 0) as complete_yn, "&VbCrlf sql = sql & " ISNULL(CASE WHEN ISNULL(VPM.p_input_dt, '') <> '' THEN VPM.p_input_dt ELSE convert(varchar(10), convert(datetime, MP.p_input_dt, 23), 120) END, '') as p_input_dt, "&VbCrlf sql = sql & " ISNULL(CASE WHEN ISNULL(VPM.p_con_date_end, '') <> '' THEN VPM.p_con_date_end ELSE convert(varchar(10), convert(datetime, MP.p_con_date_end), 120) END, '') as p_con_date_end, "&VbCrlf sql = sql & " ISNULL(CASE WHEN ISNULL(VPM.p_decide_date, '') <> '' THEN VPM.p_decide_date ELSE convert(varchar(10), convert(datetime, MP.p_decide_date, 23), 120) END, '') as p_decide_date "&VbCrlf sql = sql & " FROM V_PROJECT_METHOD VPM WITH(NOLOCK) LEFT OUTER JOIN "&VbCrlf sql = sql & " ( "&VbCrlf sql = sql & " SELECT PM.p_sn, P.p_budget, P.p_depart_dep AS pb_depart, ISNULL(PM.p_rsch_dept, P.p_depart_dep) AS p_depart_dep, "&VbCrlf sql = sql & " ISNULL(CONVERT(money, ROUND(P.p_budget * 1.0 / COUNT(*), 0)), 0) as p_cont_cost, "&VbCrlf sql = sql & " P.p_input_dt, ISNULL(ISNULL(P.p_convdate_end, P.p_convdate_end2), P.p_con_date_end) as p_con_date_end "&VbCrlf sql = sql & " , P.p_decide_date "&VbCrlf sql = sql & " FROM V_PROJECT_METHOD PM WITH(NOLOCK) LEFT OUTER JOIN V_PROJECT P WITH(NOLOCK) ON PM.p_sn = P.p_sn "&VbCrlf sql = sql & " GROUP BY PM.p_sn, PM.p_sub_type, p_depart_dep, ISNULL(PM.p_rsch_dept, P.p_depart_dep), P.p_budget, P.p_input_dt, ISNULL(ISNULL(P.p_convdate_end, P.p_convdate_end2), P.p_con_date_end) , P.p_decide_date "&VbCrlf sql = sql & " ) MP ON VPM.p_sn = MP.p_sn AND CASE ISNULL(VPM.p_rsch_dept, '') WHEN '' THEN MP.p_depart_dep ELSE ISNULL(VPM.p_rsch_dept, '') END = MP.p_depart_dep "&VbCrlf sql = sql & " LEFT OUTER JOIN "&VbCrlf sql = sql & " ( "&VbCrlf sql = sql & " SELECT p_child_sn, SUM(convert(money, est_money)) as est_money, SUM(convert(money, act_money)) as act_money "&VbCrlf sql = sql & " FROM direct_cost WITH(NOLOCK) "&VbCrlf sql = sql & " WHERE del_yes_no = 'no' "&VbCrlf sql = sql & " AND ISNULL(est_money, '') <> '' "&VbCrlf sql = sql & " AND ISNULL(act_money, '') <> '' "&VbCrlf sql = sql & " AND ISNULL(p_child_sn, '') <> '' "&VbCrlf sql = sql & " GROUP BY p_child_sn "&VbCrlf sql = sql & " ) DC ON VPM.p_child_sn = DC.p_child_sn LEFT OUTER JOIN "&VbCrlf sql = sql & " ( "&VbCrlf sql = sql & " SELECT p_sn AS p_child_sn, SUM(cost_money) AS cost_money "&VbCrlf sql = sql & " FROM V_BUSI_COST WITH(NOLOCK) "&VbCrlf sql = sql & " WHERE ISNULL(p_sn, '') <> '' "&VbCrlf sql = sql & " GROUP BY p_sn "&VbCrlf sql = sql & " ) AC ON VPM.p_child_sn = AC.p_child_sn LEFT OUTER JOIN "&VbCrlf sql = sql & " ( "&VbCrlf sql = sql & " SELECT p_sn as p_child_sn, SUM(convert(money, convert(varchar(50), target4))) AS entertain_money "&VbCrlf sql = sql & " FROM 문서내용 WITH(NOLOCK) "&VbCrlf sql = sql & " WHERE 문서제목 = '접대비보고서' "&VbCrlf sql = sql & " AND del_yes_no='no' "&VbCrlf sql = sql & " AND ISNULL(p_sn, '') <> '' "&VbCrlf sql = sql & " GROUP BY p_sn "&VbCrlf sql = sql & " ) EM ON VPM.p_child_sn = EM.p_child_sn LEFT JOIN "&VbCrlf sql = sql & " ( "&VbCrlf sql = sql & " SELECT p_child_sn FROM ( "&VbCrlf sql = sql & " SELECT p_child_sn, ROW_NUMBER() OVER(PARTITION BY p_sn ORDER BY P_DECIDE_DATE ASC) AS rnum FROM V_PROJECT_METHOD WITH(NOLOCK) WHERE p_child_sn <> '' "&VbCrlf sql = sql & " ) T WHERE rnum = 1 "&VbCrlf sql = sql & " ) PDD ON VPM.P_CHILD_SN = PDD.p_child_sn "&VbCrlf sql = sql & " WHERE ISNULL(VPM.p_child_sn, '') <> '' "&VbCrlf sql = sql & " ) RT LEFT OUTER JOIN TEAM_GROUP TG WITH(NOLOCK) ON RT.p_depart = TG.team_cd AND TG.div_use = '1' "&VbCrlf sql = sql & " WHERE ISNULL(TG.id, '') <> '' "&VbCrlf sql = sql & " ORDER BY ISNULL(TG.id, '') "&VbCrlf 'Response.write sql&"


" Rs.open sql, adoconn,adopenstatic, adlockreadonly '######################################################################################### '######################################################################################### %>