Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 17th, 2003, 04:23 PM
cruttley cruttley is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 cruttley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question SQL - summing invoice items and taxes

I have three tables - Invoice, InvoiceItem and InvoiceItemTax, all related by InvoiceNumber. InvoiceItemTax may no exist for a given InvoiceNumber.

On Invoice is a denormalized TotalAmount column.

On InvoiceItem is a TotalPrice
On InvoiceTax is a TotalTax

I want to do an integrity check to ensure the data is accurate for all invoices.

I want to ensure the Invoice.TotalAmount equals the sum of all the related InvoiceItem.TotalPrice PLUS the sum of all the related InvoiceTax.TotalTax

I can't figure out how to do this!

all help appreciated.

Reply With Quote
  #2  
Old February 19th, 2003, 04:00 AM
yassoor's Avatar
yassoor yassoor is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada - Egypt
Posts: 60 yassoor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
I hope I didn't mis-understand what you ment.
but if you want a query that would report improper values of total amount then:

In ACCESS this should work

a query like this should give you the results you want

SELECT ID
FROM [IN]
WHERE TAmount <> (
(Select sum( Price )
FROM [Item]
Where [Item].ID = [in].ID)
+
(Select sum (TAx)
FROM [Tax]
Where [Tax].ID = [in].ID)
)


For
Table IN -- Fields ID, TAmout
Table TAx -- Fields ID, TaxAmount
Table Item -- Fields ID, Price

where x is the ID of the invoice you want to validate.
The IDs returned are for invoices that do not pass the validation.

The syntax might be a little different in other DB platforms.

Note: I have only tested this using very small data sets .. so it might not be 100% effecient, please make sure you test more.
__________________
I hope this is of any help to anyone.

Yassoor
http://www.WebsitesCreation.ca

Last edited by yassoor : February 19th, 2003 at 04:09 AM.

Reply With Quote
  #3  
Old February 19th, 2003, 07:57 AM
cruttley cruttley is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 cruttley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the info. In fact, I am doing this in Oracle and i have tried the following SQL:

SELECT *
FROM inv a
WHERE total_amt <>
((Select sum( qty_num * unit_price ) FROM inv_item b Where a.inv_no = b.inv_no)
+
(Select sum (tax_amt) FROM inv_item_tax c Where a.inv_no = c.inv_no=));

It still does not work - I get a 'missing right parenthesis' error on at the + sign.

I've tried lots of combinations, but can't get around the error.

Reply With Quote
  #4  
Old February 19th, 2003, 09:39 AM
yassoor's Avatar
yassoor yassoor is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada - Egypt
Posts: 60 yassoor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
IN
SELECT *
FROM inv a
WHERE total_amt <>
((Select sum( qty_num * unit_price ) FROM inv_item b Where a.inv_no = b.inv_no)
+
(Select sum (tax_amt) FROM inv_item_tax c Where a.inv_no = c.inv_no=));

Is the very last = sign a typo?!

Reply With Quote
  #5  
Old February 19th, 2003, 12:36 PM
cruttley cruttley is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 cruttley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yes - thats a typo.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > SQL - summing invoice items and taxes


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


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





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