Hi,I have a conundrum that has perplexed me for a while whereby I need to return a pieces of data from rows on the basis that if an explicit value exists return that but if the value does not exist then we need to 'fall back' to a default value which has the value of NULL as the match.So, a manufacturer may have a range of products and, optionally (for example based on the manufacturers own rules) each product may have a code that identifies it uniquely or perhaps one code 'does all' for this manufacturer. The JOIN is therefore based on manufacturer and product, but the PRODUCT may be NULL to signify "All products", or alternatively to reflect the "default" or "fall back" code for those products not given a unique code for that manufacturer.Bear in mind that this is 'reflecting' a real situation (that I do not have control over table structure or data) and I am trying to simplfy for examples!The following code hopefully highlights the problem fully and what I am after is an elegant solution, if one exists! I am not sure if there is a simple answer (and therefore I am missing the obvious) or if in fact I need to use a derived or temporary table and effectively 'double' my query execution:The ultimate aim of the code below is to get a list of orders with associated joined data including the PRODUCT_CODE. There should be 4 rows returned - with ORDER_ID 2 only being returned once and the PRODUCT_CODE being "11111".Code:-- start standing data tablesCREATE TABLE #Retailer( RETAILER_NAME varchar(20), POSTCODE varchar(10))CREATE TABLE #Product( PRODUCT_NAME varchar(20), PRODUCT_DESCRIPTION varchar(1000), RETAILER_NAME varchar(20))CREATE TABLE #Retailer_ID_Code( RETAILER_NAME varchar(20), PRODUCT_NAME varchar(20), PRODUCT_CODE INT)INSERT INTO #Product VALUES('Shirt', 'Big Baggy Blue','Smiths')INSERT INTO #Product VALUES('Coat', 'Wild White Wool','Smiths')INSERT INTO #Product VALUES('Jumper', 'Rediculous Red','Smiths')INSERT INTO #Product VALUES('Shirt', 'Yellow','Jones')INSERT INTO #Product VALUES('Coat', 'Green','Jones')INSERT INTO #Product VALUES('Jumper', 'Purple','Jones')INSERT INTO #Product VALUES('Shirt', '100% Cotton','Williams')INSERT INTO #Product VALUES('Coat', 'Wool','Williams')INSERT INTO #Product VALUES('Jumper', 'Fleece','Williams')INSERT INTO #Retailer VALUES('Smiths', 'ZZ1 1TT')INSERT INTO #Retailer VALUES('Jones', 'AA1 1BB')INSERT INTO #Retailer VALUES('Williams', 'YY1 1CC')INSERT INTO #Retailer_ID_Code VALUES('Smiths', 'Shirt', 11111)INSERT INTO #Retailer_ID_Code VALUES('Smiths', NULL, 22222)INSERT INTO #Retailer_ID_Code VALUES('Jones', 'Jumper', 33333)INSERT INTO #Retailer_ID_Code VALUES('Jones', 'Coat', 44444)INSERT INTO #Retailer_ID_Code VALUES('Jones', 'Shirt', 555555)INSERT INTO #Retailer_ID_Code VALUES('Williams', NULL, 77777)-- end standing data tables-- Create orders dataCREATE TABLE #Orders( ORDER_ID int NOT NULL IDENTITY, PRODUCT_NAME varchar(20), RETAILER_NAME varchar(20), COST decimal(18,2))INSERT INTO #Orders VALUES ('Coat', 'Smiths',88.88)INSERT INTO #Orders VALUES ('Shirt','Smiths',55.99)INSERT INTO #Orders VALUES ('Coat', 'Jones',122.22)INSERT INTO #Orders VALUES ('Coat', 'Williams',55)-- End Create orders dataSELECT o.ORDER_ID, o.PRODUCT_NAME, o.RETAILER_NAME, p.PRODUCT_DESCRIPTION, r.POSTCODE, o.COST, rpc.PRODUCT_CODEFROM #Orders oINNER JOIN #Retailer r ON r.RETAILER_NAME = o.RETAILER_NAMEINNER JOIN #Product p ON p.PRODUCT_NAME = o.PRODUCT_NAME AND p.RETAILER_NAME = r.RETAILER_NAMEINNER JOIN #Retailer_ID_Code rpc ON rpc.RETAILER_NAME = p.RETAILER_NAME AND ( rpc.PRODUCT_NAME = p.PRODUCT_NAME OR rpc.PRODUCT_NAME IS NULL )DROP TABLE #OrdersDROP TABLE #RetailerDROP TABLE #ProductDROP TABLE #Retailer_ID_Code
Effectively I want to say: "return rows where the Retailer name and product code match, but if the product code does not match return to me the retailer ID code for the NULL row instead."The desired output would be therefore: ORDER_ID PRODUCT_NAME RETAILER_NAME PRODUCT_DESCRIPTION POSTCODE COST PRODUCT_CODE2 Shirt Smiths Big Baggy Blue ZZ1 1TT 55.99 111111 Coat Smiths Wild White Wool ZZ1 1TT 88.88 222223 Coat Jones Green AA1 1BB 122.22 444444 Coat Williams Wool YY1 1CC 55.00 77777Apologies if this appears confused... when tackling this I thought it to be quite straight forward, but actually doing it and explaining it seems quite tricky!Many thanks for any help!