我知道这个问题已被多次发布,但在谷歌上1小时后我还是无法让它工作
SELECT
ROW_NUMBER() OVER(ORDER BY x.Year DESC, x.Month) AS ID,
x.*,
Finance.fGetRates(x.XTimeStamp)
FROM
( SELECT Client,
Month(TimeStamp) As Month,
Year(TimeStamp) As Year,
Day(TimeStamp) As Day,
(dateadd(yy,(Year(TimeStamp)-1900),0)
+ dateadd(mm,Month(TimeStamp)-1,0)
+ Day(TimeStamp)-1) AS XTimeStamp,
Sum(KwTop) As KwTop,
Sum(KwHeap) as KwHeap,
Sum(KwLow) As KwLow
FROM Ori.vEnergyUnion
GROUP BY Year(TimeStamp), Month(TimeStamp),Day(TimeStamp), Client
) x
LEFT OUTER JOIN Finance.fGetRates(x.XTimeStamp) Fr
ON Fr.ValidFrom = x.XTimeStamp
ORDER BY Fr
Msg 4104,Level 16,State 1,Line 19 多部分标识符 “x.XTimeStamp”无法绑定。
任何帮助都会受到关注,谢谢。
答案 0 :(得分:3)
您无法将参数传递给JOIN
上的函数。
使用APPLY
:
SELECT ROW_NUMBER() OVER(ORDER BY x.Year DESC, x.Month) AS ID,
x.*,
fr.*
FROM (
SELECT Client,
Month(TimeStamp) As Month,
Year(TimeStamp) As Year,
Day(TimeStamp) As Day,
(dateadd(yy,(Year(TimeStamp)-1900),0)
+ dateadd(mm,Month(TimeStamp)-1,0)
+ Day(TimeStamp)-1) AS XTimeStamp,
Sum(KwTop) As KwTop,
Sum(KwHeap) as KwHeap,
Sum(KwLow) As KwLow
FROM Ori.vEnergyUnion
GROUP BY
Year(TimeStamp), Month(TimeStamp),Day(TimeStamp), Client
) x
OUTER APPLY
(
SELECT *
FROM Finance.fGetRates(x.XTimeStamp) Fr
WHERE fr.ValidFrom = x.XTimeStamp
) fr