473,378 Members | 1,544 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,378 software developers and data experts.

Aggregate query problem

Hi all,

I'm trying to create what I thought was a simple inventory query, but I'm having a problem. I have a shipment table that looks like this:

SHIP_ID SHIP_QTY
1 24
2 12

Then a table that shows when certain items from a shipment are "processed":

SHIP_ID PROCESSED_QTY
1 2
1 3

As you can see, a shipment can occur in the processed table multiple times, which leads to problems when I try to do the following query:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT s.CLI_CODE as "Client", Sum(s.SHIP_QTY) - Sum(p.PROC_QTY) AS "No. of pallets on hand"
  3. FROM SHIPMENT s, PROCESSED p
  4. WHERE s.SHIP_ID = p.SHIP_ID
  5. GROUP BY s.CLI_CODE;
  6.  
  7.  

What happens is that the shipment quantity is added twice due to the duplicate entries in the processed table, resulting in 48-5, instead of 24-5 like I want. Anyone have any ideas? I need to keep the sum on the SHIP_QTY column due to multiple shipments from the same client. Thanks!
Nov 18 '07 #1
4 1773
gintsp
36
Something like
Expand|Select|Wrap|Line Numbers
  1. SELECT cli_id, ship_id, s1 - s2 FROM
  2. (
  3.   SELECT cli_id, ship_id, sum(ship_qty) s1
  4.   FROM shipment
  5.   GROUP BY ship_id, cli_id
  6. ) a,
  7. (
  8.   SELECT ship_id, sum(proc_qty) s2
  9.   FROM processed
  10.   GROUP BY ship_id
  11. ) b
  12. WHERE a.ship_id = b.ship_id
BTW it is not clear from your description how many shipments one client may have.

Gints Plivna
http://www.gplivna.eu
Nov 18 '07 #2
Something like
Expand|Select|Wrap|Line Numbers
  1. SELECT cli_id, ship_id, s1 - s2 FROM
  2. (
  3.   SELECT cli_id, ship_id, sum(ship_qty) s1
  4.   FROM shipment
  5.   GROUP BY ship_id, cli_id
  6. ) a,
  7. (
  8.   SELECT ship_id, sum(proc_qty) s2
  9.   FROM processed
  10.   GROUP BY ship_id
  11. ) b
  12. WHERE a.ship_id = b.ship_id
BTW it is not clear from your description how many shipments one client may have.

Gints Plivna
http://www.gplivna.eu
Thanks, I originally did something like that, but as per your question there will be many shipments from certain clients. The result from that query will return onhand totals for each shipment, whereas I would like a single onhand total for each client.
Nov 18 '07 #3
amitpatel66
2,367 Expert 2GB
sbowden81,

Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Nov 19 '07 #4
amitpatel66
2,367 Expert 2GB
Thanks, I originally did something like that, but as per your question there will be many shipments from certain clients. The result from that query will return onhand totals for each shipment, whereas I would like a single onhand total for each client.
From your comments, my understanding is:

Eg:

Sample Data:

client shipment qty
1 1 24
1 2 24

shipid pro_qty
1 2
1 2
2 1
2 1

output you require is:

clientid shipmentid qtyonhand
1 1 20
1 2 22

This is what you require?
Nov 19 '07 #5

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

Similar topics

2
by: Greg Stark | last post by:
I find I often want to be able to do joins against views where the view are aggregates on a column that has an index. Ie, something like SELECT a.*, v.n FROM a JOIN (select a_id,count(*) as n...
2
by: Toby Dann | last post by:
I have an aggregate query as recordsource for a form to show a list of invoices, including the totals (calculated by the aggregate query - I'm trying to duplicate as little info as possible here)....
1
by: sausage31 | last post by:
I have a table as follows.... Device LotID Result1 Result2 Result3 aaa 1 5 10 15 bbb 1 2 4 6 aaa 2 ...
5
by: Jim | last post by:
Need help with aggregate function...for each unique product, I need the provider with the cheapest cost factor Here't the table (Table1) ID product provider factor 1 123456 abc...
10
by: neb | last post by:
Dear member of the forum, Ms access has built-in aggregate function like: -Sum, Max, First, Avg, ... Is it possible to build user-defined aggregate? (if you have any clue, do not hesitate to...
12
by: Bill Moran | last post by:
Hey all. I've hit an SQL problem that I'm a bit mystified by. I have two different questions regarding this problem: why? and how do I work around it? The following query: SELECT GCP.id,...
0
by: BillCo | last post by:
just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
5
by: BillCo | last post by:
I just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
1
by: codeWarrior007 | last post by:
Hi, i'm running a query and everytime I run the query i get the error: 'Aggregate Type' is not a valid name. Make sure that it does not include invalid characters, punctuation, or is not too...
3
by: ncsthbell | last post by:
I am pulling my hair out on this! Seems like it should be easy, I just can not get it to work like I want. I know I am doing something wrong, so I hope someone can be so kind to guide me!!! I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.