我认为我写的CTE错误,我是一位主要的新手,请您多多解释。仍然在几乎所有内容上都遇到无法约束的错误
错误消息:
无法将多部分标识符绑定为“ PS_Margin.Emp或供应商ID
不能将多部分标识符绑定为“ PS_Margin。按人员百分比计的项目利润
问题/目标:
尽管我不知道如何做到这一点,但可能需要在列名中添加别名以空格和特殊字符
WITH
Profit_Score_CTE ( [Emp or Vendor ID], [Project Profit by Person %] ) AS (
SELECT PS_Margin.[Emp or Vendor ID],
CASE
WHEN ps.[Project Profit by Person %] > .4 THEN 1
WHEN ps.[Project Profit by Person %] > .2 THEN 3
WHEN ps.[Project Profit by Person %] > .1 THEN 5
WHEN ps.[Project Profit by Person %] > .05 THEN 8
ELSE 13 END
AS [Profit Score]
FROM dbo.PS_Margin AS ps
)
SELECT emp.[Employee Name], emp.[USID], [Profit Score]*0.3 AS [Final Score]
FROM dbo.PS_Emp AS emp
left join Profit_Score_CTE
ON emp.USID = Profit_Score_CTE.[Emp or Vendor ID]
答案 0 :(得分:0)
“无法绑定”通常是指您如何使用表别名对列进行ID。尝试为您的表指定别名,并将其用于列:
WITH
--Section 1: Profit Score
Profit_Score_CTE( [Emp or Vendor ID], [Project Profit by Person %] ) AS (
SELECT PS_Margin.[Emp or Vendor ID],
CASE
WHEN ps.[Project Profit by Person %] > .4 THEN 1
WHEN ps.[Project Profit by Person %] > .2 THEN 3
WHEN ps.[Project Profit by Person %] > .1 THEN 5
WHEN ps.[Project Profit by Person %] > .05 THEN 8
ELSE 13 END
AS [Profit Score]
FROM [dbo].[PS_Margin] ps
)
答案 1 :(得分:0)
I'm guessing because you haven't provided enough information to reproduce the error. But I think the problem might be that you define the columns of your CTE here:
Profit_Score_CTE( [Emp or Vendor ID], [Project Profit by Person %] ) AS (
And yet you alias your second column with a different name here:
AS [Profit Score]
You need to decide which column name you want your CTE to expose and use it consistently.
答案 2 :(得分:0)
我假设您的CTE定义正确。您的CTE名为“ Profit_Score_CTE”。您的实际选择语句引用了对象dbo.PS_Emp和dbo.PS。我猜您应该用CTE名称替换dbo.PS。例如,
with Profit_Score_CTE as (select ...)
select emp.[Employee Name], ...
from dbo.PS_Emp as emp
left join Profit_Score_CTE
on emp.USID = Profit_Score_CTE.[Emp or Vendor ID]
order by ...
;
别名PS仅在CTE的定义内可见。不能在使用CTE的实际select语句中使用它。注意,我为您的表提供了别名并使用了它-您应该努力实现一致的编码样式。另请注意order by子句。结果集中的行没有一个就没有定义的顺序-通常很重要。并且请使用空格使代码可读并避免容易的错误。您在计算列[最终得分]的公式中填入了所有内容。
答案 3 :(得分:0)
感谢您的帮助!
我的新手功能区确实与此一起显示。该问题与SQL完全无关。我没有指向sql server中正确的数据库。
在SQL Server左上角屏幕上的“可用数据库”下拉框中,您需要选择要使用的数据库。这将使脚本能够找到您的数据库,并开始解决很多错误。