Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Left Outer Join Help

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
0
36
US
I think this should be easy but today my mind just isn't working. The UPL table has 5 records. The CPD table may have between 0 and five matching records for each CustPricingMasterFK. The following code yields the correct number of records when I use CPD.CustPricingMasterFK = 9099. In this case there are two records with CustPricingMasterFK = 9099 in the CPD table, Rental Rate (895.00) & Reefer Charge (250.00). My problem is when I want to include more records from the CPD table like this: CPD.CustPricingMasterFK >= 9099 and CPD.CustPricingMasterFK <= 9100, then I get the results as shown in the second output below. What I want is always 5 records for each CPD.CustPricingMasterFK group of records (9099, 9100, 910). Do I need to use a cursor, a derived table, or CTE of some sort?

Code:
-- * used for testing
SELECT *
FROM dbo.UnitPricingList UPL

LEFT OUTER JOIN(SELECT *
	 FROM dbo.CustomerPricingDetail CPD
	 WHERE CPD.CustPricingMasterFK = 9099) P

ON UPL.UnitPricingPK = P.UnitPricingFK

1st Output: These results are Good for CPD.CustPricingMasterFK = 9099
Rental Rate 1 8556 9099 1 895.00
Lot Charge 1 NULL NULL NULL NULL
Reefer Charge 1 11102 9099 3 250.00
Reefer Monitor 1 NULL NULL NULL NULL
Early Return Rate 1 NULL NULL NULL NULL

2nd Output: These results are NOT Good for CPD.CustPricingMasterFK >= 9099 and CPD.CustPricingMasterFK <= 9100
Rental Rate 1 8556 9099 1 895.00 8556 9099 1 895.00
Rental Rate 1 8556 9099 1 895.00 8557 9100 1 895.00
Rental Rate 1 8557 9100 1 895.00 8556 9099 1 895.00
Rental Rate 1 8557 9100 1 895.00 8557 9100 1 895.00
Rental Rate 1 11103 9100 3 250.00 8556 9099 1 895.00
Rental Rate 1 11103 9100 3 250.00 8557 9100 1 895.00
Lot Charge 1 8557 9100 1 895.00 NULL NULL NULL NULL
Lot Charge 1 11103 9100 3 250.00 NULL NULL NULL NULL
Reefer Charge 1 11102 9099 3 250.00 11102 9099 3 250.00
Reefer Charge 1 11102 9099 3 250.00 11103 9100 3 250.00
Reefer Charge 1 8557 9100 1 895.00 11102 9099 3 250.00
Reefer Charge 1 8557 9100 1 895.00 11103 9100 3 250.00
Reefer Charge 1 11103 9100 3 250.00 11102 9099 3 250.00
Reefer Charge 1 11103 9100 3 250.00 11103 9100 3 250.00
Reefer Monitor 1 8557 9100 1 895.00 NULL NULL NULL NULL
Reefer Monitor 1 11103 9100 3 250.00 NULL NULL NULL NULL
Early Return Rate 1 8557 9100 1 895.00 NULL NULL NULL NULL
Early Return Rate 1 11103 9100 3 250.00 NULL NULL NULL NULL


Auguy
Sylvania/Toledo Ohio
 
I think I have it, at least I'm close. CPM is the parent table of CPD. The cross join gives me all the potential records and the left outer join gives me the actual values. Hopefully just some cleanup and parameter substitution. Please let me know if anyone has an easier method. Thanks
Code:
SELECT UPL.UnitPricingPK, UPL.Descriptn, P.Price
	FROM dbo.UnitPricingList UPL 
	CROSS JOIN dbo.CustomerPricingMaster CPM  

	LEFT OUTER JOIN
		(SELECT CPD2.CustPricingDetailPK, CPD2.UnitPricingFK, CPD2.Price
		 ,CPM2.CustPricingMasterPK
		FROM dbo.CustomerPricingDetail CPD2
		LEFT OUTER JOIN dbo.CustomerPricingMaster CPM2
			ON CPD2.CustPricingMasterFK = CPM2.CustPricingMasterPK
		 WHERE CPM2.CustMasterFK = 6035) P
	ON
	  UPL.UnitPricingPK = P.UnitPricingFK AND
	  CPM.CustPricingMasterPK = P.CustPricingMasterPK

	WHERE CPM.CustMasterFK=6035

This yields the correct results
1 Rental Rate 895.00
2 Lot Charge NULL
3 Reefer Charge 250.00
4 Reefer Monitor NULL
5 Early Return Rate NULL
1 Rental Rate 895.00
2 Lot Charge NULL
3 Reefer Charge 250.00
4 Reefer Monitor NULL
5 Early Return Rate NULL
1 Rental Rate 895.00
2 Lot Charge NULL
3 Reefer Charge 250.00
4 Reefer Monitor NULL
5 Early Return Rate NULL


Auguy
Sylvania/Toledo Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top