SQL - 创建具有多个 CTE 的视图
SQL - Creating View with multiple CTEs
我要创建一个视图,它有 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]
要指定多个步骤 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]
?
我要创建一个视图,它有 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]
要指定多个步骤 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]
?