我有这个查询
SELECT *
FROM
(SELECT
TT.RECID, TT.TRANSDATE,
TT.DATAAREAID, TT.SOURCERECID,
TT.SOURCECURRENCYCODE, TT.TAXAMOUNT,
TT.TAXBASEAMOUNT, TT.TAXBASEAMOUNTCUR,
TT.TAXAMOUNTCUR, TT.TAXORIGIN,
TT.VOUCHER, TT.TAXITEMGROUP,
TT.TAXCODE, TT.SOURCEBASEAMOUNTCUR,
TT.SOURCETAXAMOUNTCUR,
TTGJAERIVA.TAXTRANSRELATIONSHIP,
TTGJAERIVA.GENERALJOURNALACCOUNTENTRY,
TTGJAERIVA.LEDGERDIMENSION,
GJAERIVA.TEXT,
GJAERIVA.LEDGERDIMENSION AS LEDGERDIMENSIONGAE,
GJAERIVA.POSTINGTYPE
FROM
TAXTRANS TT
INNER MERGE JOIN
TAXTRANSGENERALJOURNALACCOUNTENTRY TTGJAERIVA ON TTGJAERIVA.TAXTRANS = TT.RECID
INNER MERGE JOIN
GENERALJOURNALACCOUNTENTRY GJAERIVA ON TTGJAERIVA.GENERALJOURNALACCOUNTENTRY = GJAERIVA.RECID
AND GJAERIVA.POSTINGTYPE IN (14, 236, 71, 41)
WHERE
(TT.TAXORIGIN <> 11
AND TT.TRANSDATE BETWEEN '2015-01-01' AND '2015-04-30')
OR
(TT.TAXORIGIN = 11
AND TT.TRANSDATE BETWEEN '2015-01-01' AND '2015-04-30' )) AS T
---- --JOIN
LEFT MERGE JOIN
(SELECT
SOURCERECID, TAXITEMGROUP, TAXCODE,
GENERALJOURNALACCOUNTENTRY, VOUCHER, TAXAMOUNT,
TAXAMOUNTCUR
FROM
T) TTRIVA ON T.VOUCHER = TTRIVA.VOUCHER
AND T.SOURCERECID = TTRIVA.SOURCERECID
AND TT.TAXITEMGROUP = TTRIVA.TAXITEMGROUP
AND (TTRIVA.TAXCODE LIKE 'RIVA%')
AND TTRIVA.GENERALJOURNALACCOUNTENTRY = TTGJAE.GENERALJOURNALACCOUNTENTRY
我收到错误:
无效的对象名称'T'。
进入子查询,就在左合并连接之后(在查询的最后)
---- --JOIN
LEFT MERGE JOIN
(SELECT
SOURCERECID, TAXITEMGROUP, TAXCODE,
GENERALJOURNALACCOUNTENTRY, VOUCHER,
TAXAMOUNT, TAXAMOUNTCUR
FROM
T) TTRIVA ON T.VOUCHER = TTRIVA.VOUCHER
AND T.SOURCERECID = TTRIVA.SOURCERECID
AND TT.TAXITEMGROUP = TTRIVA.TAXITEMGROUP
AND (TTRIVA.TAXCODE LIKE 'RIVA%')
AND TTRIVA.GENERALJOURNALACCOUNTENTRY = TTGJAE.GENERALJOURNALACCOUNTENTRY
如果我在加入之前使用'T'作为查询结果,我不知道为什么会出现这种情况?此致
答案 0 :(得分:0)
与往常一样,仔细格式化有助于揭示SQL中的错误 句法。
SELECT * FROM (
SELECT
TT.RECID,
-- etc
GJAERIVA.POSTINGTYPE
FROM TAXTRANS TT
INNER MERGE JOIN TAXTRANSGENERALJOURNALACCOUNTENTRY TTGJAERIVA
ON TTGJAERIVA.TAXTRANS = TT.RECID
INNER MERGE JOIN GENERALJOURNALACCOUNTENTRY GJAERIVA
ON TTGJAERIVA.GENERALJOURNALACCOUNTENTRY = GJAERIVA.RECID
AND GJAERIVA.POSTINGTYPE IN( 14,236,71,41
) -- as ????
WHERE
(TT.TAXORIGIN <> 11 AND
TT.TRANSDATE BETWEEN '2015-01-01' AND '2015-04-30')
OR (TT.TAXORIGIN = 11 AND
TT.TRANSDATE BETWEEN '2015-01-01' AND '2015-04-30')
) AS T
---- --JOIN
LEFT MERGE JOIN --- etc.
第一个)
子句之前的结束WHERE
可能不是
你想要的。
SQL是一种声明性语言。如果你做的话,它会更容易使用
声明尽可能清楚地告诉读者。例如,通过重新格式化查询,我们可以很容易地看到WHERE
子句可以简化为
WHERE TT.TRANSDATE BETWEEN '2015-01-01' AND '2015-04-30'
因为每个TT.TAXORIGIN
等于或等于11。