我有以下查询,我在 2 个表中搜索商品价格
select productSerial, productName
coalesce(
(select top 1 price
from productPrice pp
where pp.id = wi.id
order by pp.date),
(select top 1 price
from productPriceEast ppe
where ppe.id = wi.id
order by ppe.date)
) as baseprice
from warehouseItems wi
但现在我也想要描述,如何更改查询以获得多于 1 列?我无法将查询更改为此,因为合并会失败。我正在考虑选择合并到临时表中的这些查询,并进行某种连接而不是合并,不确定这是否是正确的方法。
select top 1 price, productDescrip
from productPriceEast
where ppe.id = wi.id
order by ppe.date
看起来你需要一个
OUTER APPLY
该
COALESCE()
函数接受两个以上的参数。例如,以下是有效语法:不确定您实际上陷入了什么?...如果您可以提供表定义、一些示例数据和示例预期输出,它可能会让您的目标更加清晰。
例如,
COALESCE()
如果您的目标是将 theprice
与 theproductDescrip
一起显示,则可以在子句的同一表达式中将两个子查询连接在一起,如下所示:假设您的
ORDER BY ppe.date
子句对于给定的 是确定性的ppe.id
。或者更简单,
CONCAT()
原始子查询表达式中的两个字段如下所示: