INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Left Outer Join Help

Left Outer Join Help

(OP)
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

RE: Left Outer Join Help

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close