以下是查询的一部分。如果我正在执行除此之外我得到了正确的输出。 但如果我包含此查询,我收到错误:
Msg 8155,Level 16,State 2,Procedure SP_CUR_LEAR,294行 没有为'CTE_DATE'的第1列指定列名。
此外,我需要在表#lear1中插入数据,没有日期空白。
;WITH CTE_DATE AS (
SELECT @start_date
UNION ALL
SELECT @start_date + 1
FROM CTE_DATE
WHERE @start_date <= @End_Date
),
CTE AS
(
Select CTE_DATE.St_Date eff_date
From CTE_Date
),
C (eff_date, partnumber, total_price) as
(select CTE.eff_date, PARTNUMBER, ISNULL(X.cnt,(Select Z.cnt
From (Select AA.Totalprice cnt, Rank() Over(Order By AA.eff_date asc) RN
From #LEAR AA
Where AA.eff_date < CTE.eff_date OR AA.eff_date = CTE.eff_date
) Z
Where Z.RN = 1))
from CTE
Left Outer Join (Select eff_date, partnumber, totalprice cnt From #lear) X ON X.eff_date = CTE.eff_date
)
insert into #lear1 (eff_date, partnumber, totalprice)
select eff_date, partnumber, total_price from c
答案 0 :(得分:0)
您错过了CTE_DATE
的别名。为St_Date
添加别名@start_date
并在其他地方处理相同名称将解决此问题。
如果您想添加Day + 1,可以使用DATEADD
;WITH CTE_DATE AS (
SELECT @start_date AS St_Date
UNION ALL
SELECT DATEADD(DAY, +1, St_Date) AS St_Date
FROM CTE_DATE
WHERE St_Date <= @End_Date
),
CTE AS
(
Select CTE_DATE.St_Date eff_date
From CTE_Date
),
C (eff_date, partnumber, total_price) as
....
....