0

我有一个要创建的视图,它有 2 个 CTES。这就是我在下面所做的。第一部分很好,但是在“AND”之后事情就不起作用了。感谢您的意见

CREATE VIEW dbo.VW.SPAg
AS With today as 
(SELECT * FROM dbo.Work_Days
 WHERE [Date] = CAST(GETDATE() AS DATE)
)

AND WITH rd as
(SELECT [DATE] AS REP_DATE
 FROM dbo.Link_Days
 WHERE DAY ([DATE]) = 1
)
SELECT wm *,
gr.DATE_ORDINAL AS Goods_Rcvd_Ordinal
gt.DATE_ORDINAL AS Goods_Trnpt_Ordinal
today.DATE

FROM dbo.SPAg sg
INNER JOIN rd
  ON YEAR(sg.Client_Query)= YEAR(rd.REP_DATE)
LEFT JOIN dbo.Work_DAYS gr
ON sg.Goods_Rcvd = gr.[DATE]
LEFT JOIN dbo.Work_DAYS gt
ON sg.Goods_Trnpt = gt.[DATE]
4

1 回答 1

5

要指定多个步骤CTE使用,

CREATE VIEW dbo.VW.SPAg   /* [dbo].[VW_SPAg] Probably you want this name*/
AS 
WITH today as 
(SELECT * FROM dbo.Work_Days
 WHERE [Date] = CAST(GETDATE() AS DATE)
), rd as
(SELECT [DATE] AS REP_DATE
 FROM dbo.Link_Days
 WHERE DAY ([DATE]) = 1
)
SELECT wm *,
gr.DATE_ORDINAL AS Goods_Rcvd_Ordinal
gt.DATE_ORDINAL AS Goods_Trnpt_Ordinal
today.DATE
FROM dbo.SPAg sg
INNER JOIN rd
  ON YEAR(sg.Client_Query)= YEAR(rd.REP_DATE)
LEFT JOIN dbo.Work_DAYS gr
  ON sg.Goods_Rcvd = gr.[DATE]
LEFT JOIN dbo.Work_DAYS gt
  ON sg.Goods_Trnpt = gt.[DATE];

另外我不喜欢视图名称dbo.VW.SPAg,你有数据库[dbo]吗?或者应该是[dbo].[VW_SPAg]

于 2015-09-23T15:29:55.217 回答