MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
  #1  
Old December 3rd, 2003, 09:01 AM
friendly_state friendly_state is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 11 friendly_state User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Query rounding errors and compensation

Kind of new to SQL so let me set this up a bit.
I am writing a month end billing report in query analyzer for orders that have invoices processed within the date range. The results are then being sent to a file.
For every order there can be up to 5 account numbers billed based on an allocated percentage of the invoiced amount for that account number. The hitch is since I’m dealing with percentages there are occasionally rounding errors.

How can I add the calculated totals and compare them with the invoice amount, if there is a difference then add/subtract the amount to the calculated total with the highest percent?

I’m pulling from two tables Orders and Invoices.

“Orders” fields: Acct1, Pct1, Acct2, Pct2, Acct3, Pct3, Acct4, Pct4, Acct5, Pct5, Order_Nbr

"Invoices" fields: Invoice_Timestamp, Invoice_Amt, Order_Nbr

There are 5 selects union’d together like this, but to save space I’ll only show the first 2.
@StartDate = first of the month @MyDate=last day of the month
-----------------------------------------------------------------------------------

SELECT
CAST(h.Acct1 AS CHAR(6))
+ CAST(k.Order_Nbr AS CHAR(6))
+ Replace((Replace(CAST(k.Invoice_Amt * (h.Pct1/100) as DECIMAL(6,2)),'.','')),'-','')
FROM Invoices AS k
left join Orders AS h ON k.Order_Nbr = h.Order_Nbr

WHERE k. Invoiced_Timestamp <= @MyDate and
k. Invoiced_Timestamp >= @MyStartDate and
k. Invoice_Amt <> '0' and
h.Acct1 <> ''

UNION ALL SELECT
CAST(h.Acct2 AS CHAR(6))
+ CAST(k.Order_Nbr AS CHAR(6))
+ Replace((Replace(CAST(k.Invoice_Amt * (h.Pct2/100) as DECIMAL(6,2)),'.','')),'-','')

FROM Invoices AS k
left join Orders AS h ON k.Order_Nbr = h.Order_Nbr

WHERE k. Invoiced_Timestamp <= @MyDate and
k. Invoiced_Timestamp >= @MyStartDate and
k. Invoice_Amt <> '0' and
h.Acct2 <> ''

Thanks in advance for any help.

Last edited by friendly_state : December 3rd, 2003 at 09:04 AM.

Reply With Quote
  #2  
Old December 3rd, 2003, 10:18 AM
friendly_state friendly_state is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 11 friendly_state User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I am still interested in any ideas, but what I decided to do

add two fields to the Invoice table
Adjustment_Acct = 1, 2, 3, 4,or 5
Adjustment_Amt = difference in calculated and actual

I'll calculate and insert these when invoices are entered. Then when I'm running the billing statement I can do case statements for each acct. Like this

***Adjust for first account****
Case (when k.Adjustment_Acct = 1) then
Replace((Replace(CAST((k.Invoice_Amt * (h.Pct1/100) + k.Adjustment_Amt) as DECIMAL(6,2)),'.','')),'-','')
Else
Replace((Replace(CAST((k.Invoice_Amt * (h.Pct1/100) as DECIMAL(6,2)),'.','')),'-','')
End

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Query rounding errors and compensation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway