473,395 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Comparing SUM of two tables

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

✓ answered by Rabbit

You need to flip your conditional. Right now you're only returning the ones that received more than they ordered.

7 2506
Rabbit
12,516 Expert Mod 8TB
You need to flip your conditional. Right now you're only returning the ones that received more than they ordered.
Dec 15 '11 #2
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 Expert Mod 8TB
What's does your current SQL look like?
Dec 19 '11 #4
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
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 Expert Mod 8TB
That should work. Post some sample data of the records that should show up but aren't showing up.
Dec 19 '11 #7
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
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...
4
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...
0
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)...
2
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: ...
1
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...
3
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...
1
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...
1
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
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.