473,549 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing SUM of two tables

5 New Member
Hi Everyone,
This one is driving me crazy.

I have three tables: purchase_order, purchase_order_ bom, and rog_bom.

The table purchase_order_ bom is a list of parts ordered for the purchase order. The table rog_bom is a list of parts received for the purchase order (ROG stands for receipt of goods).

I want to run a query that looks through table purchase_order but only show the purchase orders that have received less parts than the total number of parts order. Basically the purchase orders that haven't been fully received.

table purchase_order:
ID INT

table purchase_order_ bom:
ID INT
po_ID INT
quantity INT
product_ID INT

table rog_bom:
ID INT
po_ID INT
quantity INT
product_ID INT

I thought it would be something like this (but this shows only one purchase_order row):

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   *,
  3.   po.ID AS po_ID
  4. FROM
  5.   purchase_order po
  6. WHERE
  7.   (SELECT SUM(quantity) AS po_bom_total FROM purchase_order_bom po_bom WHERE po_bom.po_ID=po.ID) < (SELECT SUM(quantity) FROM rog_bom WHERE rog_bom.po_ID=po.ID)
  8.  
Thank you!
Andy
Dec 15 '11 #1
7 2510
Rabbit
12,516 Recognized Expert Moderator MVP
You need to flip your conditional. Right now you're only returning the ones that received more than they ordered.
Dec 15 '11 #2
tracerstevens
5 New Member
Hi Rabbit,

Good eye, I've changed it around so that ROG total is first. So it checks if ROG_bom < PO_bom. But it still only shows one PO, which actually has the ROG_bom total equeal to PO_bom, so it doesn't make sense.

There should be about 30 PO's that have less items received than ordered.

Am I writing the query correctly? Or am I totally off base?

Thanks!
Andy
Dec 19 '11 #3
Rabbit
12,516 Recognized Expert Moderator MVP
What's does your current SQL look like?
Dec 19 '11 #4
tracerstevens
5 New Member
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.  *,
  3.  po.ID AS po_ID
  4. FROM
  5.  purchase_order po
  6. WHERE
  7.  (SELECT SUM(quantity) FROM rog_bom WHERE rog_bom.po_ID=po.ID) < (SELECT SUM(quantity) AS po_bom_total FROM purchase_order_bom po_bom WHERE po_bom.po_ID=po.ID)
  8.  
Is it clear what I'm trying to produce, or is that confusing as well?

Thank you,
Andy
Dec 19 '11 #5
tracerstevens
5 New Member
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.  *,
  3.  po.ID AS po_ID
  4. FROM
  5.  purchase_order po
  6. WHERE
  7.  (SELECT SUM(quantity) FROM rog_bom WHERE rog_bom.po_ID=po.ID) < (SELECT SUM(quantity) AS po_bom_total FROM purchase_order_bom po_bom WHERE po_bom.po_ID=po.ID)
  8.  
Is it clear what I'm trying to produce, or is that confusing as well?

Thank you,
Andy
Dec 19 '11 #6
Rabbit
12,516 Recognized Expert Moderator MVP
That should work. Post some sample data of the records that should show up but aren't showing up.
Dec 19 '11 #7
tracerstevens
5 New Member
Rabbit,
As I was putting together some sample data for you I figured out that the query above does work but not when I applied it to my real data.

I found out that if there are no ROGs, it returns NULL instead of 0, so I had to use:

Expand|Select|Wrap|Line Numbers
  1. SELECT COALESCE(SUM(quantity),0) FROM rog_bom WHERE po_ID=po.ID
Thanks for your help, you got me to the solution
Dec 21 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4191
by: Bung | last post by:
Hi, I am not really familiar with MS ACCESS. However, I am trying to make some reports where you compare current data from a table with old data from the same table. What I'm saying is we have a database with current data, and the same database with old data. I need to create reports for new rows inserted, deleted rows, and updates...
4
4146
by: osmethod | last post by:
Hello, I have read many articles about comparing tables, like - loops, delete queries, appending to temp table with index etc Problem: 2 tables T1 & T2. Data is suppposed to be the same in both tables but i need to check that this is so. (Maybe a user deleted a row in one table only or changed a value in a field!). The tables have no...
0
1675
by: laxmiuk | last post by:
I've original version of a table called PMM (Product Material Master). Thro' a web interface, user can change that table contents. Once changed, i need to raise an ECN (Engineering CHange Note) specifying what changes happened to original PMM table whether rows are deleted, new rows are added or existing rows are modified etc. I've both old...
2
3081
by: Wayne | last post by:
We have a query which compares each field in two tables (let's say Table1 and Table2) and identifies any records that have been modified. For example, one of these columns looks like: Field: Priority Table: Table2 Sort: Show: Criteria: <>.
1
1416
by: razaqtelecom | last post by:
Hi, I want to know how can i write a stored procedure for the following sceario. Take if there are three tables, table1, table2, table3 and table1 is linked with table2 and table 2 is linked with table3. Now i have to compare a data in table 3 for one of the rows in table1 where as For the same row in table1 there will 100 rows in table 2...
3
2033
by: hasnain | last post by:
Dear All, I want to compare two tables. Both of the tables have same fields but their location in terms of columns can be different. My target is to get those fields only whose values are found different on comparing. Regards Hasnain
1
2287
by: jerkyjerk | last post by:
Ei guys..newbie in programming..just want to ask how will I compare the contents of particular cell of the table(from parent window) to the contents of a cell of the table (child window)? how will I use ID for this? I need to compare because If the two contents are the same, no need to add the table (from the child window) to the table on the...
1
1124
by: ckmoied | last post by:
Hi, Can I have a script to compare the two tables. Such that I can identify the missing rows of Table A in Table B, in the absece of any know key.
5
1840
by: Franck | last post by:
how come unchanged always true even if data changed This code come from my saving button: ============================================ DataSet ds1 = new DataSet(); DataSet ds2 = new DataSet(); DataSet ds3 = new DataSet(); //Static Dataset which contain values when my form load
4
6387
by: gillianbrooks91 | last post by:
Forgive me for asking this question, I've trawled through nearly every available post on this subject that I can find for a few weeks now but nothing quite points me in the right direction. I'm quite new to trying to mess around with VB and ADO within MS Access and have realised the steep learning curve I have, but, I want to try and solve...
0
7524
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6048
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.