我已经在数据库中创建了一些表,我想通过从所有这些表中选择列来创建单个视图。
这是我的查询:
CREATE VIEW dbo.View_Name
AS
SELECT a.column1, a.column2, b.column1, b.column2, c.column1, c.column2
FROM
table1 AS a LEFT OUTER JOIN
table2 AS b ON a.column1 = b.column1 LEFT OUTER JOIN
table3 AS c ON b.column2 = c.column2
WHERE
(a.[Load_Date] = (select max(Load_Date)
From table1)
and (b.[Load_Date] = (select max(Load_Date)
From table2)
and (c.[Load_Date] = (select max(Load_Date)
From table3)
go
当我尝试使用多个where子句并完成查询时,它向我抛出错误“'go'附近的语句不正确”。
我是SQL Server的新手,我被困在这里。谁能帮我完成查询而没有错误?
答案 0 :(得分:0)
您在WHERE中的括号混乱了-试试这个
CREATE VIEW dbo.View_Name
AS
SELECT a.column1, a.column2, b.column1, b.column2, c.column1, c.column2
FROM
table1 AS a LEFT OUTER JOIN
table2 AS b ON a.column1 = b.column1 LEFT OUTER JOIN
table3 AS c ON b.column2 = c.column2
WHERE
(a.[Load_Date] = (select max(Load_Date) From table1))
and (b.[Load_Date] = (select max(Load_Date) From table2))
and (c.[Load_Date] = (select max(Load_Date) From table3))
go
答案 1 :(得分:0)
您可以重写您的内容,如下所示:
WITH T1 AS
(SELECT TOP 1 WITH TIES
Column1,
Column2
FROM Table1
ORDER BY Load_Date DESC),
T2 AS
(SELECT TOP 1 WITH TIES
Column1,
Column2
FROM Table2
ORDER BY Load_Date DESC),
T3 AS
(SELECT TOP 1 WITH TIES
Column1,
Column2
FROM Table3
ORDER BY Load_Date DESC)
SELECT *
FROM T1
JOIN T2 ON T1.Column1 = T2.Column1
JOIN T3 ON T2.Column1 = T2.Column2;
请注意,在您引用JOIN
中来自LEFT JOIN
的对象时,我使用了WHERE
,使它们隐含了INNER JOIN
。