declare @por_no char(8), @por_seq int
declare @amt decimal(16,4)
select @por_no = 'P2506009', @amt = 4698297087, @por_seq = 1

update HPOIMS.dbo.gm_por_detail set unitp = @amt, amt = @amt, w_unitp = @amt, w_amt = @amt, t_amt = @amt where por_no = @por_no and por_seq = @por_seq


update
	HPOIMS.dbo.ys_pbudget
set
	x_amt = (b.soyo_amt+b.input_amt)
from
	HPOIMS.dbo.ys_pbudget a
join
(
	select 
		*
	from
	(
		select
			a.pjt_no, a.main, a.sub, a.bud_seq
			, sum(a.bud_amt) bud_amt
			, sum(a.bud_x_amt) bud_x_amt
			, sum(a.bud_e_amt) bud_e_amt
			, sum(a.soyo_amt) soyo_amt
			, sum(a.input_amt	) input_amt
		from
		(
			SELECT 
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ISNULL(a.closing_yn,'N')
				, bud_amt = ISNULL(a.amt,0)
				, bud_x_amt = ISNULL(a.x_amt,0)
				, bud_e_amt = ISNULL(a.e_amt,0)
				, soyo_amt = 0
				, input_amt = 0      
			FROM 
				HPOIMS.dbo.ys_pbudget a
	
			UNION ALL
			SELECT
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ''
				, bud_amt = 0
				, bud_x_amt = 0
				, bud_e_amt = 0
				, soyo_amt = SUM(ISNULL(a.w_amt,0))
				, input_amt = 0
			FROM 
				HPOIMS.dbo.gm_por_detail a
			GROUP BY 
				a.pjt_no, a.main, a.sub, a.bud_seq
	
			UNION ALL
			SELECT
				  pjt_no = a.pjt_no
				, main = a.main
				, sub = a.sub
				, bud_seq = a.bud_seq
				, closing_yn = ''
				, bud_amt = 0
				, bud_x_amt = 0
				, bud_e_amt = 0
				, soyo_amt = 0
				, input_amt = SUM(ISNULL(a.amt,0))
			FROM 
				HPOIMS.dbo.ys_direct_cost a
			GROUP BY 
				a.pjt_no, a.main, a.sub, a.bud_seq
		) a
		GROUP BY 
			a.pjt_no, a.main, a.sub, a.bud_seq
	) a
	where
		a.bud_x_amt <> (a.soyo_amt+a.input_amt)
) b
on
	a.pjt_no = b.pjt_no
	and a.main = b.main
	and a.sub = b.sub
	and a.bud_seq = b.bud_seq
 	and a.pjt_no like '%' 
 	and a.main <> 'AB00'
	and a.curr = 'WON'
	and ROUND(isnull(a.x_amt,0)*a.base_rate,0) <> (b.soyo_amt+b.input_amt)

