0
我有這個疑問錯誤查詢
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
我得到一個錯誤:
Invalid object name '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'作爲查詢結果之前加入?問候
您可能想要分解SQL。這將成爲未來的維護噩夢。 – dhiman
相信我,它現在是一場噩夢@DhimanBhattacharya – Ledwing
備註:特別是在SQL Server上,請[不要使用'BETWEEN'](http://sqlblog.com/blogs/aaron_bertrand/archive/2011/10/19/)什麼-DO之間和最魔鬼具有功能於common.aspx)。我不完全相信'transdate'是一個日期,而不是一個完整的時間戳 –