+ -
当前位置:首页 → 问答吧 → 非常具有挑战的update语句,你能想出来么?我蛋疼了……

非常具有挑战的update语句,你能想出来么?我蛋疼了……

时间:2011-12-08

来源:互联网

不知如何说起,我把两个表摆出来吧:
Emp_Tracking表,列几条数据:
AccountNum, SubAccount, AccountType, LastDateWithoutAmount, LastDateWithAmount
————— ————— ————— ——————————— ——————————
100 0 M NULL NULL
100 1 M NULL NULL
100 2 M NULL NULL
100 3 M NULL NULL
100 4 M NULL NULL
100 5 M NULL NULL
101 0 M NULL NULL
101 1 M NULL NULL
101 2 M NULL NULL
101 3 M NULL NULL


Billing_SalesInvoice_Master表,列一些数据:
AccountNum,SubAccount,AccountType, PurchaseDate, InvoiceAmount
---------- --------- ---------- ------------- -------------
1 100 0 M 2011-12-01 00:00:00.000 200.00
2 100 1 M 2011-12-01 00:00:00.000 400.00
3 100 1 M 2011-12-05 00:00:00.000 600.00
4 100 2 M 2011-10-07 00:00:00.000 100.00
5 101 0 M 2011-12-03 00:00:00.000 0.00
6 101 1 M 2011-12-10 00:00:00.000 0.00
7 101 1 M 2011-12-01 00:00:00.000 200.00
8 101 2 M 2011-12-03 00:00:00.000 0.00
9 101 2 M 2011-12-10 00:00:00.000 0.00
10 101 0 M 2011-12-15 00:00:00.000 0.00
11 100 2 M 2011-10-09 00:00:00.000 100.00

我要修改Emp_Tracking表中 LastDateWithoutAmount, LastDateWithAmount这两个字段的值,当在
Billing_SalesInvoice_Master表中具有相同的AccountNum,SubAccount,AccountType并且InvoiceNum=0
时,我要把Billing_SalesInvoice_Master表中的最大的PurchaseDate的值给Emp_Tracking表的LastDateWithoutAmount字段,
如:101 - 0 - M这条记录,应该更新LastDateWithoutAmount=2011-12-15 00:00:00.000

当Invoice > 0时,则更新 LastDateWithAmount字段的值,
如:101 - 1 - M这条记录,应该更新LastDateWithAmount=2011-12-10 00:00:00.000

不知道大家看明白了否?这个批量更新的sql语句,纠结我一天了,蛋疼,提示:不能用游标,因为数据很多,会卡死……



作者: yangchun1213   发布时间: 2011-12-08

InvoiceNum字段在哪

作者: pengxuan   发布时间: 2011-12-08