我有以下查询
SELECT Case
when V.[Code Article] in
(SELECT [Code Article]
FROM [Prix Revient MPX CMR] c
WHERE CONVERT(DATE, V.[Date Time]) = [DATE] and [CMR] = V.[Code Site] )
then c.[Prix de revient réel]
ELSE V.[Prix de Revient]
end as [Prix de Revient]
FROM dbo.[Sales] V
我收到以下错误:
多部分标识符“c.Prix derevientréel”无法绑定
如何修改?
答案 0 :(得分:4)
如果您将查询格式化为:
SELECT (Case when V.[Code Article] in (SELECT c.[Code Article]
FROM [Prix Revient MPX CMR] c
WHERE CONVERT(DATE, V.[Date Time]) = c.[DATE] and c.[CMR] = V.[Code Site]
)
then c.[Prix de revient réel]
ELSE V.[Prix de Revient]
end) as [Prix de Revient]
FROM dbo.[Sales] V;
那么问题就很明显了。 c
子句没有then
。大概你想要一个join
:
select coalesce(c.[Prix de revient réel], V.[Prix de Revient]) as [Prix de Revient]
from dbo.[Sales] V left join
[Prix Revient MPX CMR] c
on CONVERT(DATE, V.[Date Time]) = c.[DATE] and
c.[CMR] = V.[Code Site] and
V.[Code Article] = c.[Code Article]
答案 1 :(得分:0)
您必须使用sub-query
或join
尝试以下
SELECT CASE WHEN V.[Code Article] IN (SELECT [Code Article]
FROM [Prix Revient MPX CMR] c
WHERE CONVERT(DATE, V.[Date Time]) = [DATE] and [CMR] = V.[Code Site]
)
THEN (SELECT [Prix de revient réel] FROM [Prix Revient MPX CMR] WHERE CONVERT(DATE, V.[Date Time]) = [DATE] and [CMR] = V.[Code Site])
ELSE V.[Prix de Revient]
END AS [Prix de Revient]
FROM dbo.[Sales] V