473,320 Members | 2,035 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,320 software developers and data experts.

Group By with 2 outer joins

Hi, Everybody !

I have a small problem.

If I run a query with group by having two outer joins, there, group by is not working i suppose.

Case:
table 1 : Product master having product info
table 2 & 3 : receipt master and transaction having prod_id and qty
table 4 & 5 : despatch master and transaction having prod_id and qty

Requirement:
A Single query which fetches one record from master, multiple records from receipt and despatch because the transaction table is having multiple records for the same product

Query:
select m.prod_id, isnull(sum(R.RECEIVED_QTY),0) receipt, isnull(sum(D.DESPATCH_QTY),0) despatch
from product_master m
left outer join RECEIPT_TR r on m.prod_id=r.prod_id
left outer join DESPATCH_TR d on m.prod_id=r.prod_id
group by r.prod_id, d.prod_id

Problem:
If i have 2 rows in despatch, the ISSUED column doubles the sum of d.ISSUED,
If i have 3 rows, it give thrice the sum and so on.....

Pls Help Me !!
Feb 7 '08 #1
3 1811
nedu
65
Hi,

Try this query. It may work as per u r requirement . .

Expand|Select|Wrap|Line Numbers
  1. select m.prod_id, isnull(sum(R.RECEIVED_QTY),0) receipt, isnull(sum(D.DESPATCH_QTY),0) despatch
  2. from product_master m
  3. left outer join RECEIPT_TR r on m.prod_id=r.prod_id
  4. left outer join DESPATCH_TR d on m.prod_id=d.prod_id
  5. group by m.prod_id

Regards,
Nedu. M
Feb 7 '08 #2
Hello all

I too face similar problem but unable to find a solution .
Suppose in my query i am trying to fetch more than one fields then it forces me to includes all those fields in the group by clause too.

How come it seems to be working fine?

Can you provide a solution

Thanks
bonnie

Hi,

Try this query. It may work as per u r requirement . .

Expand|Select|Wrap|Line Numbers
  1. select m.prod_id, isnull(sum(R.RECEIVED_QTY),0) receipt, isnull(sum(D.DESPATCH_QTY),0) despatch
  2. from product_master m
  3. left outer join RECEIPT_TR r on m.prod_id=r.prod_id
  4. left outer join DESPATCH_TR d on m.prod_id=d.prod_id
  5. group by m.prod_id

Regards,
Nedu. M
Mar 27 '08 #3
Delerna
1,134 Expert 1GB
Hello all

I too face similar problem but unable to find a solution .
Suppose in my query i am trying to fetch more than one fields then it forces me to includes all those fields in the group by clause too.

How come it seems to be working fine?

Can you provide a solution

Thanks
bonnie
Hi bonnie
There is no solution. These queries are called agregate queries and the simple fact is that every field selected in an aggregate query must be aggregated.
It dosn't make sense to aggregate only some fields and not others.
GROUP BY is an aggregate function so any field in your selection that you don't want to be SUM() or MIN() or MAX() or AVG() or any of the other aggregate functions must be put into the GROUP BY clause.
Thats just the way it is.
Mar 28 '08 #4

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

Similar topics

0
by: Preston Landers | last post by:
Hello all. I am trying to write a query that "just" switches some data around so it is shown in a slightly different format. I am already able to do what I want in Oracle 8i, but I am having...
8
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
1
by: gnog | last post by:
hello we have 3 tables bugs bugid | bugname custom_field_value bugid | fieldid | fieldvalue
1
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
1
by: Frank via DBMonster.com | last post by:
Hello, My ODBC driver from DB2 connect does not allow outer joins. Is this normal? Does anyone know how to configure it so it allows outer joins? Greetings, Frank
5
by: Bob Stearns | last post by:
When I run the following query with the two sections commented out, the response time is between 1 an 2 seconds; with the first indicated section enabled, the response goes up to 15 seconds even...
2
by: Randy Harris | last post by:
How can I join two tables on more than one field? In the query grid if I do that I get the ambiguous outer join error. I "kludged" it by creating a query for each table with a field computed from...
1
by: imranpariyani | last post by:
Hi i have a severe performance problem with one of my views which has 6 to 8 joins .. any help will be appreciated.. the view is: CREATE OR REPLACE VIEW thsn.trade_view AS SELECT...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.