SQL的新手。我一直在寻找有关此问题的答案,并找到了所找到的示例,我已将查询镜像为类似于示例,但此查询仍然无法正常工作。我想做的就是联接两个子查询,但是这两个子查询都在内部包含一个子查询。我收到以下错误:
信息156,第15层,状态1,第26行 关键字“开”附近的语法不正确。
这是查询,请协助,谢谢。
Select *
From
(
Select * From(
Select x.ID, x.Date, x.USOHist, x.OVXHist,
Abs(Cast((((x.USOHist / NullIf((y.USOHist),0))-1)*100) as Decimal(10,2))) AS '%USOH',
Abs(Cast((((x.OVXHist / NullIf((y.OVXHist),0))-1)*100) as Decimal(10,2))) AS '%OVXH'
From (Select a.Date as aDate, Max(b.Date) As aPrevDate From USO_OVX_Hist a Inner Join USO_OVX_Hist b on a.Date > b.Date
Group By a.Date) Sub1
Inner Join USO_OVX_Hist x on Sub1.aDate = x.Date
Inner Join USO_OVX_Hist y on Sub1.aPrevDate = y.Date
) Sub2
Inner Join
(
Select * From(
Select z.ID, z.ID2, z.Date, z.USO as USOP, z.OVX as OVXP,
Cast(((z.USO / NullIf((q.USO),0)- 1) * 100) as Decimal(10,2)) AS '%USOP',
Cast(((z.OVX / NullIf((q.OVX),0)- 1) * 100) as Decimal(10,2)) AS '%OVXP'
From (Select c.Date as cDate, Max(d.Date) As cPrevDate From USO_OVX_Price c Inner Join USO_OVX_Price d on c.Date > d.Date
Group By c.Date) Sub3
Inner Join USO_OVX_Price z on Sub3.cDate = z.Date
Inner Join USO_OVX_Price q on Sub3.cPrevDate = q.Date
) Sub4
On Sub2.Date = Sub4.Date
答案 0 :(得分:0)
在sub2
和sub4
之前添加了右括号,并添加了新别名t2
和t4
:
select * from
(
Select * From(
Select x.ID, x.Date, x.USOHist, x.OVXHist,
Abs(Cast((((x.USOHist / NullIf((y.USOHist),0))-1)*100) as Decimal(10,2))) AS '%USOH',
Abs(Cast((((x.OVXHist / NullIf((y.OVXHist),0))-1)*100) as Decimal(10,2))) AS '%OVXH'
From (Select a.Date as aDate, Max(b.Date) As aPrevDate From USO_OVX_Hist a Inner Join USO_OVX_Hist b on a.Date > b.Date
Group By a.Date) Sub1
Inner Join USO_OVX_Hist x on Sub1.aDate = x.Date
Inner Join USO_OVX_Hist y on Sub1.aPrevDate = y.Date
) t2 ) sub2
Inner Join
(
Select * From (
Select z.ID, z.ID2, z.Date, z.USO as USOP, z.OVX as OVXP,
Cast(((z.USO / NullIf((q.USO),0)- 1) * 100) as Decimal(10,2)) AS '%USOP',
Cast(((z.OVX / NullIf((q.OVX),0)- 1) * 100) as Decimal(10,2)) AS '%OVXP'
From (Select c.Date as cDate, Max(d.Date) As cPrevDate From USO_OVX_Price c Inner Join USO_OVX_Price d on c.Date > d.Date
Group By c.Date) Sub3
Inner Join USO_OVX_Price z on Sub3.cDate = z.Date
Inner Join USO_OVX_Price q on Sub3.cPrevDate = q.Date
) t4 ) sub4
On sub2.Date = sub4.Date
答案 1 :(得分:0)
使用以下内容:
SELECT *
FROM
(
SELECT *
FROM
(
SELECT
x.id,
x.date,
x.usohist,
x.ovxhist,
ABS(CAST((((x.usohist / NULLIF((y.usohist), 0)) - 1) * 100) AS decimal(10, 2))) AS '%USOH',
ABS(CAST((((x.ovxhist / NULLIF((y.ovxhist), 0)) - 1) * 100) AS decimal(10, 2))) AS '%OVXH'
FROM
(
SELECT
a.date AS adate,
MAX(b.date) AS aprevdate
FROM uso_ovx_hist a
INNER JOIN uso_ovx_hist b
ON a.date > b.date
GROUP BY a.date
) Sub1
INNER JOIN uso_ovx_hist x
ON sub1.adate = x.date
INNER JOIN uso_ovx_hist y
ON sub1.aprevdate = y.date
) Sub2
INNER JOIN
(SELECT * FROM
(
SELECT
z.id,
z.id2,
z.date,
z.uso AS usop,
z.ovx AS ovxp,
CAST(((z.uso / NULLIF((q.uso), 0) - 1) * 100) AS decimal(10, 2)) AS '%USOP',
CAST(((z.ovx / NULLIF((q.ovx), 0) - 1) * 100) AS decimal(10, 2)) AS '%OVXP'
FROM
(SELECT
c.date AS cdate,
MAX(d.date) AS cprevdate
FROM uso_ovx_price c
INNER JOIN uso_ovx_price d
ON c.date > d.date
GROUP BY c.date) Sub3
INNER JOIN uso_ovx_price z
ON sub3.cdate = z.date
INNER JOIN uso_ovx_price q
ON sub3.cprevdate = q.date
) Sub4
) t
ON sub2.date = t.date
) t2
您可以使用在线Instant SQL Formatter格式化SQL。
非常感谢。我很难理解有关添加别名以使联接起作用的要点。您介意向我解释吗?我还是SQL的新手。另外,如何精确地执行更多子查询或在该查询上联接另一个表?
另一个:在Sub4.Date上的InnerJoin NYSEData = NYSEData.Date
我收到一条错误消息,指出无法绑定Sub4对象?
我知道我需要在查询中添加另一个select和from standard来添加另一个表或子查询吗?请帮忙,谢谢。