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 |
|
das
Starting Member
8 Posts |
Posted - 2002-07-15 : 10:22:24
|
| I have table similar to the following that is created from a download.Create Table tblTransactions( upcharge int, accountcode int, std_cost numeric(10,5), amount numeric(18,0), ysnVarianceRec bit, ysnDebitCredit bit)There can be 3 records to create a balanced entry. The variance record is the difference between the std_cost of a debit and a credit.If you take the amount * std_cost of the debit versus the amount*std_Cost of the credit it should be zero. Sometimes due to rounding the entry is off +-1.00. I need to update the nonvariance record by a +-1.00 to bring the entry in balance.Ex. Upcharge AccountCode stdCost Amount Debit/Credit Varinace 123 123 .800 80 0 0 123 123 .600 60 1 0 123 123 .200 20 1 1The above is a balance entry but lets say the following happened .Ex. Upcharge AccountCode stdCost Amount Debit/Credit Varinace 123 123 .800 80 0 0 123 123 .600 -61 1 0 123 123 .200 -20 1 1I would need to add 1.00 to debit amount of 80 to bring to 81 to balance the entry. I can write the select query with group by and get the differencesSelect sum(amount)from tblTransaction group by upcharge, AccountCodebut have trouble write the update to go through this table and adjust entries.Thanks in AdvanceDerek |
|
|
joldham
Wiseass Yak Posting Master
300 Posts |
Posted - 2002-07-15 : 12:07:23
|
| das,You could do something like this (assuming the CREDIT side is always correct).UPDATE tblTransactionsSet tblTransactions.amount = tblTransactions.amount - b.amountFROM tblTransactions, (Select upcharge, AccountCode, sum(amount) amount from tblTransactions group by upcharge, AccountCode) bWHERE tblTransactions.upcharge = b.upchargeAND tblTransactions.AccountCode = b.AccountCodeAND tblTransactions.ysnDebitCredit = 0Let me know if this is not what you are looking for.Jeremy* This also assumes that you always have one DEBIT amount, no matter how many credit amounts. If this is not the case (i.e. 2 DEBITs and 2 CREDITs), then this statement will not work.*Edited by - joldham on 07/15/2002 12:09:19 |
 |
|
|
das
Starting Member
8 Posts |
Posted - 2002-07-15 : 12:47:15
|
| We can't always assume credit side is correct. It maybe the debit side is correct. You can assume that there would be only 1 to many whether it be the debit or the credit. 1 debit 2 or more credits or 1 credit 2 or more debits. |
 |
|
|
joldham
Wiseass Yak Posting Master
300 Posts |
Posted - 2002-07-15 : 14:38:00
|
| Setting up sample dataCreate Table tblTransactions (upcharge int, accountcode int, std_cost numeric(10,5), amount numeric(18,0), ysnVarianceRec bit, ysnDebitCredit bit) INSERT INTO tblTransactions SELECT 123, 123, .800, 80, 0, 0 UNION ALL SELECT 123, 123, .600, -61, 0, 1 UNION ALL SELECT 123, 123, .200, -20, 1, 1 UNION ALL SELECT 124, 123, .800, 80, 0, 0 UNION ALL SELECT 124, 123, .600, -62, 0, 1 UNION ALL SELECT 124, 123, .200, -20, 1, 1 UNION ALL SELECT 125, 123, .800, 80, 0, 1 UNION ALL SELECT 125, 123, .600, -62, 0, 0 UNION ALL SELECT 125, 123, .200, -20, 1, 0 Now Update either the debit or credit that has only one entry to equal the remaining part of the transaction that has more than one entry.UPDATE tblTransactionsSet tblTransactions.amount = tblTransactions.amount - bb.amountFROM tblTransactions, (Select a.upcharge, a.AccountCode, a.ysnDebitCredit, b.amount FROM (Select upcharge, AccountCode, ysnDebitCredit FROM tblTransactions GROUP BY upcharge, AccountCode, ysnDebitCredit Having Count(*) = 1) a INNER JOIN (Select upcharge, AccountCode, sum(amount) amount from tblTransactions group by upcharge, AccountCode) b ON a.upcharge = b.upcharge AND a.AccountCode = b.AccountCode) bbWHERE tblTransactions.upcharge = bb.upchargeAND tblTransactions.AccountCode = bb.AccountCodeAND tblTransactions.ysnDebitCredit = bb.ysnDebitCreditFinal results from above data.123 123 .80000 81 0 0123 123 .60000 -61 0 1123 123 .20000 -20 1 1124 123 .80000 82 0 0124 123 .60000 -62 0 1124 123 .20000 -20 1 1125 123 .80000 82 0 1125 123 .60000 -62 0 0125 123 .20000 -20 1 0If this doesn't work correctly, let me know what isn't right so I can modify. Another suggestion I have is to add a bit field within the tblTransactions table to indicate whether or not this update has ran. By doing this, you could modify this query to only update transactions that have not been previously updated. Jeremy |
 |
|
|
das
Starting Member
8 Posts |
Posted - 2002-07-16 : 07:08:08
|
| Thanks for your all of help Jeremy. This appears to be working fine. |
 |
|
|
|
|
|
|
|