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.
Author |
Topic |
sandesh.ravi
Posting Yak Master
110 Posts |
Posted - 2011-10-05 : 06:13:59
|
I have 2 tables All loads, Balances with the following columns and data. ID is identity column.All loadsID CardProduct Amount 1 A 102 A 20 3 A 304 B 405 B 506 B 607 C 708 C 809 A 90BalancesCardProduct BalanceA 500B 600C 400I need to write a stored proc or function that sum up the amount based on the card product in the all loads table and updates in the balances table.However next time it executes it should not choose the records that has already summed up. In other words only the new records has to be summed up.Please help me with the query.Thanks,Sandesh |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-05 : 06:26:17
|
do you've any other column in all loads table to mark that it has already been counted? if not, you need add one. else you will not be able to identify already counted ones and will end up double counting them.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sandesh.ravi
Posting Yak Master
110 Posts |
Posted - 2011-10-05 : 06:33:45
|
I can not modify the structure of the table. What about having a lookup table with id and card product. So every time when the proc execute it inserts the max id for each card product. so that every time it executes it checks only the id greater than that value.Is this a good approach?Thanks,Sandesh |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-05 : 06:36:44
|
quote: Originally posted by sandesh.ravi I can not modify the structure of the table. What about having a lookup table with id and card product. So every time when the proc execute it inserts the max id for each card product. so that every time it executes it checks only the id greater than that value.Is this a good approach?Thanks,Sandesh
ok that should be better. dont put max id but rather put all ids that have been consolidated yet. and do a left join with it next time and look for nulls to exclude already inserted ones------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sandesh.ravi
Posting Yak Master
110 Posts |
Posted - 2011-10-05 : 06:39:23
|
Could you let me know the query please.Thanks,Sandesh |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-05 : 07:28:58
|
[code]update tset t.Balance = t1.Totfrom [Balances] tinner join (select CardProduct,sum(Amount) As Tot from [All loads] a left join newtable n on n.ID = a.ID and n.CardProduct = a.CardProduct where n.ID is null group by a.CardProduct)t1on t1.CardProduct = t.CardProductinsert into newtableselect columns...from [All loads][/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sandesh.ravi
Posting Yak Master
110 Posts |
Posted - 2011-10-06 : 05:45:13
|
Thank you Visakh...Thanks,Sandesh |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-06 : 09:00:48
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|