WITH
EmpDetails(EmpCode)
AS
(
SELECT EmpCode
FROM Employee.tblEmp_Demographics
WHERE RecordStatus='present'
)
select *,ROW_NUMBER() OVER (PARTITION BY EmpCode ORDER BY DateTaken DESC) AS RowNum,
(select GradeName From Master.GradeMaster where GradeCode=Grade And Deleted=0) As Grade,
SUM(Amount) OVER(PARTITION BY Grade) AS EmpAmount,
SUM(DependentAmount) OVER(PARTITION BY Grade) As DepAmount,
0 As CheckNo
from [dbo].[SELECT_AIRTICKET_DETAILS_BY_DATE](EmpDetails.EmpCode,getdate(),1) WHERE YEAR(DateTaken)=year(getdate())
但是我收到了错误
Msg 4104,Level 16,State 1,Line 11无法绑定多部分标识符“EmpDetails.EmpCode”。
答案 0 :(得分:0)
我认为问题是您将列EmpDetails.EmpCode
名称传递给表值函数而不是单个值的结果。你可以试试这个:
DECLARE @EmpCode nvarchar(100);
SELECT TOP 1
@EmpCode = EmpCode
FROM Employee.tblEmp_Demographics
WHERE RecordStatus='present';
select *,ROW_NUMBER() OVER (PARTITION BY EmpCode ORDER BY DateTaken DESC) AS RowNum,
(select GradeName From Master.GradeMaster where GradeCode=Grade And Deleted=0) As Grade,
SUM(Amount) OVER(PARTITION BY Grade) AS EmpAmount,
SUM(DependentAmount) OVER(PARTITION BY Grade) As DepAmount,
0 As CheckNo
from [dbo].[SELECT_AIRTICKET_DETAILS_BY_DATE](@EmpCode,getdate(),1) WHERE YEAR(DateTaken)=year(getdate());
如果你想要Employee.tblEmp_Demographics
表中的多个值的结果,你可以遍历每个值(可能不是最好的),或者修改你的表值函数以返回包含所有EmpCode值的结果集,然后添加WHERE
子句以根据需要过滤数据。
修改强> 的
或许这样的事情 - 请注意修改后的功能,它会返回所有EmpCodes&参数少一个:
select *,ROW_NUMBER() OVER (PARTITION BY EmpCode ORDER BY DateTaken DESC) AS RowNum,
(select GradeName From Master.GradeMaster where GradeCode=Grade And Deleted=0) As Grade,
SUM(Amount) OVER(PARTITION BY Grade) AS EmpAmount,
SUM(DependentAmount) OVER(PARTITION BY Grade) As DepAmount,
0 As CheckNo
FROM [dbo].[SELECT_AIRTICKET_DETAILS_BY_DATE](getdate(),1) AS [Tickets]
INNER JOIN Employee.tblEmp_Demographics
ON tblEmp_Demographics.EmpCode = [Tickets].EmpCode
AND tblEmp_Demographics.RecordStatus = 'present'
WHERE YEAR(DateTaken) = year(getdate());
使用此解决方案,该函数将返回所有EmpCode,但我们通过在INNER JOIN
表上执行Employee.tblEmp_Demographics
来过滤掉不必要的EmpCode。