You can create a pseudo table with UNNEST for your parameter and do a left outer join:
CopySELECT t.row1, t.row2, t.row3
FROM UNNEST(ARRAY[my_id]) i LEFT OUTER JOIN table t ON t.id = i;
Answer from clemens on Stack OverflowYou can create a pseudo table with UNNEST for your parameter and do a left outer join:
CopySELECT t.row1, t.row2, t.row3
FROM UNNEST(ARRAY[my_id]) i LEFT OUTER JOIN table t ON t.id = i;
Another solution would be to simply search the table three times
CopySELECT
COALESCE( (SELECT row1 FROM table WHERE id=my_id), NULL),
COALESCE( (SELECT row2 FROM table WHERE id=my_id), NULL),
COALESCE( (SELECT row3 FROM table WHERE id=my_id), NULL)
coalesce more than 1 column in sql server - Database Administrators Stack Exchange
python 3.x - Pandas combine/coalesce multiple columns into 1 - Stack Overflow
Coalesce usage in left outer join
How to coalesce two tables with identical structure?
Videos
Looks like you need an OUTER APPLY
select
wi.productSerial,
wi.productName
isnull(pp.price, ppe.price) as baseprice,
isnull(pp.description, ppe.description) as description
from warehouseItems wi
outer apply (
select top (1)
pp.price,
pp.description
from productPrice pp
where pp.id = wi.id
order by pp.date
) pp
outer apply (
select top (1)
ppe.price,
ppe.description
from productPriceEast ppe
where ppe.id = wi.id
order by ppe.date
) ppe;
The COALESCE() function accepts more than two parameters. For example, the following is valid syntax:
SELECT COALESCE(Expression1, Expression2, Expression3, Expression4, Etc)
Not sure what you're actually stuck on?...if you could provide your table definitions, some sample data, and example expected output, it may make your goal more clear.
For example, you can concatenate two subqueries together in the same expression of the COALESCE() clause, if your goal is to show the price with the productDescrip together, like so:
SELECT
productSerial,
productName,
COALESCE
(
(
SELECT TOP 1 price
FROM productPrice pp
WHERE pp.id = wi.id
ORDER BY pp.date
),
CONCAT
(
(
SELECT TOP 1 price
FROM productPriceEast ppe
WHERE ppe.id = wi.id
ORDER BY ppe.date
),
' ',
(
SELECT TOP 1 productDescrip
FROM productPriceEast ppe
WHERE ppe.id = wi.id
ORDER BY ppe.date
)
)
) as baseprice
FROM warehouseItems wi
Assuming your ORDER BY ppe.date clause is deterministic for a given ppe.id.
Or even simpler, CONCAT() the two fields inside the original subquery expression like so:
SELECT
productSerial,
productName,
COALESCE
(
(
SELECT TOP 1 price
FROM productPrice pp
WHERE pp.id = wi.id
ORDER BY pp.date
),
(
SELECT TOP 1 CONCAT(price, ' ', productDescrip) AS priceAndProductDescrip
FROM productPriceEast ppe
WHERE ppe.id = wi.id
ORDER BY ppe.date
)
) as baseprice
FROM warehouseItems wi