在我的查询中使用左连接时出错

时间:2013-06-14 13:38:26

标签: sql sql-server

我的查询给出了以下错误消息:

 15:36:08  [SELECT - 0 row(s), 0.000 secs]  [Error Code: 102, SQL State: 37000]  [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'c'.

查询:

SELECT ftgnamn, ordernr, bestdat, q_text, oac, bcu, reminv, oldfak, reminv+oldfak
FROM
(
    (SELECT fr.ftgnamn, q_order.q_ordernr 'ordernr', q_order.kundbestdat 'bestdat', q_order.q_text, q_order.q_orderpos, 
            q_order.vbordsum 'oac', q_order.ordsum 'bcu', (q_order.vbordsum - q_order.vb_faktsum) 'reminv'
    FROM    fr, q_order, ac
    WHERE   fr.ftgnr=q_order.ftgnr AND q_order.vbordsum - q_order.vb_faktsum > 0
    ) a LEFT JOIN 
    (
    SELECT SUM(belaconto) 'oldfak', q_ordernr, q_orderpos
    FROM ac
    WHERE CAST('2000-05-31' AS DATETIME) <= ac.faktdat AND ac.faktdat IS NOT NULL
    GROUP BY q_ordernr, q_orderpos
    ) b ON (a.q_ordernr=b.q_ordernr AND a.q_orderpos=b.q_orderpos)
) c

有人能看出出了什么问题吗?

1 个答案:

答案 0 :(得分:2)

让我为你分解一下。此示例返回完全相同的错误,因为您从非子查询中选择:

select 
    * 
from 
(
-- missing a select here!
-- select * from --uncomment this line
(
    select  1 as x
) a 
left join
(
    select 2 as y
) b
    on a.x=b.y
) c