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?
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
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