Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Left Outer Join - too many results

Author  Topic 

schicmg
Starting Member

2 Posts

Posted - 2008-10-15 : 18:10:22
I'm new to this... The following query is intended to populate a dispensing record report. Desired result is a line for each item on a bill of material, and if an allocation exists, the allocation details. IF no allocation exists, blank space to write in the data. The left outer join is returing duplicate entries and mixing data bewteen the two tables. Any pointers?

Mike

SELECT BP62FPRO.FMA.MORD,
BP62FPRO.FMA.MPROD,
BP62FPRO.FMA.MQREQ,
BP62FPRO.ELAL01.AORD,
BP62FPRO.ELAL01.APROD,
BP62FPRO.ELAL01.ALOC,
BP62FPRO.ELAL01.ALOT,
BP62FPRO.ELAL01.LACNTR,
BP62FPRO.IIML01.IDESC,

FROM BP62FPRO.FMA,

BP62FPRO.IIML01
Left Outer Join "BP62FPRO"."ELAL01" on( "BP62FPRO"."FMA"."MORD" = "BP62FPRO"."ELAL01"."AORD" )

WHERE (BP62FPRO.FMA.MPROD = BP62FPRO.IIML01.IPROD ) and
(BP62FPRO.FMA.MPROD = BP62FPRO.ELAL01.APROD ) AND
(BP62FPRO.FMA.MORD = BP62FPRO.ELAL01.AORD)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-15 : 18:19:16
You'll need to show us some sample data in the tables and the expected result set using that sample data. The sample data should be illustrative of your issue and doesn't necessarily have to be real data.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-16 : 01:47:56
quote:
Originally posted by schicmg

I'm new to this... The following query is intended to populate a dispensing record report. Desired result is a line for each item on a bill of material, and if an allocation exists, the allocation details. IF no allocation exists, blank space to write in the data. The left outer join is returing duplicate entries and mixing data bewteen the two tables. Any pointers?

Mike

SELECT BP62FPRO.FMA.MORD,
BP62FPRO.FMA.MPROD,
BP62FPRO.FMA.MQREQ,
BP62FPRO.ELAL01.AORD,
BP62FPRO.ELAL01.APROD,
BP62FPRO.ELAL01.ALOC,
BP62FPRO.ELAL01.ALOT,
BP62FPRO.ELAL01.LACNTR,
BP62FPRO.IIML01.IDESC,

FROM BP62FPRO.FMA,

BP62FPRO.IIML01
Left Outer Join "BP62FPRO"."ELAL01" on( "BP62FPRO"."FMA"."MORD" = "BP62FPRO"."ELAL01"."AORD" )

WHERE (BP62FPRO.FMA.MPROD = BP62FPRO.IIML01.IPROD ) and
(BP62FPRO.FMA.MPROD = BP62FPRO.ELAL01.APROD ) AND
(BP62FPRO.FMA.MORD = BP62FPRO.ELAL01.AORD)




does more than allocation exists for any line item? i.e is relationship 1 to many? if else you need to user GROUP BY to return 1 line per item
Go to Top of Page

schicmg
Starting Member

2 Posts

Posted - 2008-10-16 : 09:09:09
Thanks for your interest.
Sample data:
FMA (order bill of material)
MORD MPROD MQREQ
112422 1 100
112422 2 100
112422 3 400

ELAL01 (allocations for order)
AORD APROD ALOT ALOC
112422 1 A wbb
112422 1 B wbb
112422 2 c wbb


IIML01 (product master table)
IPROD IDESC
1 paper
2 plastic
3 tote

desired result

MPROD IDESC MQREQ A LOT ALOC
1 paper 100 A wbb
B wbb
2 plastic 100 C wbb
3 tote 400

current result
MPROD IDESC MQREQ A LOT ALOC
1 paper 100 A wbb
1 paper 100 B wbb
1 paper 100 C wbb
2 plastic 100 A wbb
2 plastic 100 B wbb
2 plastic 100 C wbb
and all the various combinations
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-16 : 09:31:00
Start by rewriting your query according to ANSI style
SELECT		FMA.MORD,
FMA.MPROD,
FMA.MQREQ,
ELAL01.AORD,
ELAL01.APROD,
ELAL01.ALOC,
ELAL01.ALOT,
ELAL01.LACNTR,
IIML01.IDESC,
FROM BP62FPRO.FMA
INNER JOIN BP62FPRO.IIML01 ON IIML01.IPROD = FMA.MPROD
LEFT JOIN BP62FPRO.ELAL01 ON ELAL01.AORD = FMA.MORD
AND ELAL01.APROD = FMA.MPROD
AND ELAL01.AORD = FMA.MORD



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-16 : 12:31:59
quote:
Originally posted by schicmg

Thanks for your interest.
Sample data:
FMA (order bill of material)
MORD MPROD MQREQ
112422 1 100
112422 2 100
112422 3 400

ELAL01 (allocations for order)
AORD APROD ALOT ALOC
112422 1 A wbb
112422 1 B wbb
112422 2 c wbb


IIML01 (product master table)
IPROD IDESC
1 paper
2 plastic
3 tote

desired result

MPROD IDESC MQREQ A LOT ALOC
1 paper 100 A wbb
B wbb
2 plastic 100 C wbb
3 tote 400

current result
MPROD IDESC MQREQ A LOT ALOC
1 paper 100 A wbb
1 paper 100 B wbb
1 paper 100 C wbb
2 plastic 100 A wbb
2 plastic 100 B wbb
2 plastic 100 C wbb
and all the various combinations



seems like this is for reporting. If yes, the format that you're looking for can be easily achieved at front end. Just return data as it is now and use hide duplicates or suppress duplicates option available in reports. Which report application are you using?
Go to Top of Page
   

- Advertisement -