user2316116
user2316116

Reputation: 6814

LEFT JOIN on two or more tables

What should be the best approach to return a list of products with either default values or custom user values? Example:

Products

product_id   name              category
---------------------------------------
PROD1        SM-W620NZKBDBT    2 
SOFT1        MS-UIOA2189090    2
SOFT2        LE-UIOSAI91000    2

Settings (default settings, that can be used by any user - user_id is nulll)

product_id   title           color    user_id
----------------------------------------------
PROD1        Samsung         black    NULL
SOFT1        MS Office-365   NULL     NULL

Settings (user specific settings, built on top of default settings)

product_id   title           color    user_id
----------------------------------------------
PROD1        Samsung/WIFI    dark     123
SOFT3        MS Windows 10   N/A      123 

*user settings expected to be in the same table, if possible

Expected output of product list for user with user_id=123 and category=2

product_id   title           color     
-------------------------------------- 
PROD1        Samsung/WIFI    dark      <- product with user settings
SOFT1        MS Office-365   NULL      <- product with default settings
SOFT2        LE-UIOSAI91000  NULL      <- product without settings

Product list and settings will have very few items and a limited usage.

Upvotes: 1

Views: 92

Answers (3)

Ilyes
Ilyes

Reputation: 14928

Try this:

Declare @Products Table (product_id varchar(25) , name varchar(50) , category int);
Declare @Settings Table (product_id varchar(25) , title varchar(50) , color varchar(25) , user_id int);

Insert into @Products values
('PROD1' , 'SM-W620NZKBDBT' , 2) , 
('SOFT1' , 'MS-UIOA2189090' , 2) ,
('SOFT2' , 'LE-UIOSAI91000' , 2) ;

Insert into @Settings values
('PROD1' , 'Samsung' , 'black' , Null),
('SOFT1' , 'MS Office-365' , Null , Null),
('PROD1' , 'Samsung/WIFI' , 'Dark' , 123),
('SOFT3' , 'MS Windows 10' , 'N/A' , 123);


SELECT A.product_id product_id ,
       coalesce(A.Name,B.title) Tile,
       B.Color
From @Products A Left join @Settings B ON A.product_id = B.product_id and USER_ID = 123;

Demo.

Upvotes: 0

Chanukya
Chanukya

Reputation: 5893

CREATE TABLE #Table1
    ([product_id] varchar(5), [name] varchar(14), [category] int)
;

INSERT INTO #Table1
    ([product_id], [name], [category])
VALUES
    ('PROD1', 'SM-W620NZKBDBT', 2),
    ('SOFT1', 'MS-UIOA2189090', 2),
    ('SOFT2', 'LE-UIOSAI91000', 2)
;
CREATE TABLE #Table2
    ([product_id] varchar(5), [title] varchar(13), [color] varchar(5), [user_id] varchar(4))
;

INSERT INTO #Table2
    ([product_id], [title], [color], [user_id])
VALUES
    ('PROD1', 'Samsung', 'black', NULL),
    ('SOFT1', 'MS Office-365', NULL, NULL)

CREATE TABLE #Table3
    ([product_id] varchar(5), [title] varchar(13), [color] varchar(4), [user_id] int)
;

INSERT INTO #Table3
    ([product_id], [title], [color], [user_id])
VALUES
    ('PROD1', 'Samsung/WIFI', 'dark', 123),
    ('SOFT3', 'MS Windows 10', 'N/A', 123)

SELECT 
A.PRODUCT_ID,ISNULL(B.TITLE,A.NAME) TITLE,B.COLOR 
FROM #TABLE1 A  LEFT JOIN 
 #TABLE3 B ON A.PRODUCT_ID=B.PRODUCT_ID  
 AND USER_ID = 123
ORDER BY  user_id DESC

Upvotes: 0

Mikhail Lobanov
Mikhail Lobanov

Reputation: 3026

SELECT TOP (1) WITH TIES
    *
FROM products       P
LEFT JOIN settings  S ON S.product_id = P.product_id AND (S.user_id = @user_id OR S.user_id IS NULL)
ORDER BY ROW_NUMBER() OVER(PARTITION BY P.product_id ORDER BY S.user_id DESC)

Upvotes: 1

Related Questions