Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Confusing Update

Author  Topic 

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-12-19 : 20:44:15


I need to insert/update the table tbl_emp_payment from tbl_payments
This is kind of a daily process :

Day1 - We will be inserting the values for the 'E123456' to tbl_emp_payment
Day2 - We will delete from tbl_payments and reload the second insert and now I need to update the pending
amount in a different way
Crieria : If any one of the value changes in the pending amount of the E123456
Then all the current amount should be populated in the pending amount where it is 0
and any thing greater than 0 should take only the pending amount

Current amount is updated as it is

Day 3:Now in Day3 we have one extra insert and one update So again for insert also it should meet the
Day2 criteria where the new record has a value in the pending amount which is greater than 0 then
it should update the pending amount(which is greater than 0) with the current amount



create Table tbl_payment

(
emp_number Varchar(20) NOT NULL,
payment_description varchar(10) NOT NULL,
current_payment money NOT NULL,
pending_payment money NOT NULL
)


create Table tbl_emp_payment

(
emp_payment_id INT IDENITITY(1,),
emp_number Varchar(20) NOT NULL,
payment_description varchar(10) NOT NULL,
current_payment money NOT NULL,
pending_payment money NOT NULL
)

Day 1:

INSERT INTO tbl_payments
(
emp_number,
payment_description,
current_payment,
pending_payment
)

SELECT 'E123456','COUNTY',456.78,0.00 UNION
SELECT 'E123456','CITY',893.56,0.00 UNION
SELECT 'E123456','STATE',234.34,0.00 UNION
SELECT 'E123456','MISC',789.78,0.00


Day2 :

DELETE FROM tbl_payments

INSERT INTO tbl_payments
(
emp_number,
payment_description,
current_payment,
pending_payment
)

SELECT 'E123456','COUNTY',456.78,0.00 UNION
SELECT 'E123456','CITY',893.56,0.00 UNION
SELECT 'E123456','STATE',234.34,28.00 UNION
SELECT 'E123456','MISC',789.78,0.00


Day3 :

DELETE FROM tbl_payments

INSERT INTO tbl_payments
(
emp_number,
payment_description,
current_payment,
pending_payment
)

SELECT 'E123456','COUNTY',456.78,0.00 UNION
SELECT 'E123456','LTG',389.23,73.00



sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-12-19 : 23:13:32
Can somebody help me on this.Please let me know if the above description is not clear
Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-12-19 : 23:42:06
Help !!!
Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-12-20 : 01:03:37
When I use this it works for some cases but not for every cases :

UPDATE tbl_emp_payment
SET
current_payment = b.current_payment,
pending_payment = case
when (b.pending_payment > a.pending_payment or b.pending_payment < a.pending_payment) THEN a.pending_payment
else
a.current_payment
end
FROM
tbl_emp_payment a
INNER JOIN tbl_payment b ON
a.emp_number = b.emp_number AND
a.payment_description = b.payment_description
Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-12-20 : 01:59:19
Is there any better way of optimizing the query :

UPDATE tbl_emp_payment
SET
current_payment = b.current_payment,
pending_payment = CASE
WHEN a.pending_payment = 0 AND b.pending_payment > 0 THEN b.pending_payment
WHEN a.pending_payment > 0 AND b.pending_payment = 0 THEN a.pending_payment
WHEN a.pending_payment > 0 AND b.pending_payment > 0 THEN b.pending_payment
WHEN b.current_payment > 0 AND a.pending_payment = 0 AND b.pending_payment =0 THEN b.current_payment
ELSE
b.pending_payment
END
FROM
tbl_emp_payment a
INNER JOIN tbl_payment b ON
a.emp_number = b.emp_number AND
a.payment_description = b.payment_description
Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2006-12-20 : 16:13:46
Can somebody please help ???
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-12-20 : 16:19:42
You didn't provide the expected result set in your intial post, so it's hard for us to help.

Tara Kizer
Go to Top of Page
   

- Advertisement -