我有一个类似的查询:
-- --JOIN one
LEFT MERGE JOIN
(SELECT TT.TAXAMOUNT,TT.TAXCODE,TT.TAXITEMGROUP,TT.VOUCHER,TT.TAXORIGIN, TTF.SOURCERECID
FROM TAXTRANS TT
INNER MERGE JOIN TAXTABLE TAX ON TAX.PAYMENTTAXCODE = TT.TAXCODE AND TAX.DATAAREAID = 'vb'
INNER MERGE JOIN TAXTRANS TTF ON TAX.TAXCODE = TTF.TAXCODE AND TTF.SOURCEDOCUMENTLINE = TT.SOURCEDOCUMENTLINE
WHERE TT.TRANSDATE between @FECHA_INI AND @FECHA
AND TT.TAXORIGIN = 11 AND ("+@Codigo_IEPSCUOTAP_TT+")) TTIEPSCUOTA ON
C1.TT_VOUCHER = TTIEPSCUOTA.VOUCHER
AND C1.TT_TAXITEMGROUP = TTIEPSCUOTA.TAXITEMGROUP
AND PRO.TTF_SOURCERECID = TTIEPSCUOTA.SOURCERECID
--JOIN two
LEFT MERGE JOIN
(SELECT TT.TAXAMOUNT,TT.TAXCODE,TT.TAXITEMGROUP,TT.VOUCHER,TT.TAXORIGIN, TTF.SOURCERECID
FROM TAXTRANS TT
INNER MERGE JOIN TAXTABLE TAX ON TAX.PAYMENTTAXCODE = TT.TAXCODE AND TAX.DATAAREAID = 'vb'
INNER MERGE JOIN TAXTRANS TTF ON TAX.TAXCODE = TTF.TAXCODE AND TTF.SOURCEDOCUMENTLINE = TT.SOURCEDOCUMENTLINE
WHERE TT.TRANSDATE between @FECHA_INI AND @FECHA
AND TT.TAXORIGIN = 11 AND ("+@Codigo_IEPS25P_TT+")) TTIEPS25 ON
C1.TT_VOUCHER = TTIEPS25.VOUCHER
AND C1.TT_TAXITEMGROUP = TTIEPS25.TAXITEMGROUP
AND PRO.TTF_SOURCERECID = TTIEPS25.SOURCERECID
正如您所看到的,Join one
和Join two
是相同的连接,除了最后4行,所以我决定创建一个临时表来调用select一次,如:
IF OBJECT_ID('tempdb..##TTC' ) IS NOT NULL DROP TABLE ##TTC
GO
SELECT * INTO ##TTC FROM (
SELECT
TT.TAXAMOUNT,
TT.TAXCODE,
TT.TAXITEMGROUP,
TT.VOUCHER,
TT.TAXORIGIN,
TT.SOURCERECID,
FROM TAXTRANS TT
INNER MERGE JOIN TAXTABLE TAX ON TAX.PAYMENTTAXCODE = TT.TAXCODE AND TAX.DATAAREAID = 'vb'
INNER MERGE JOIN TAXTRANS TTF ON TAX.TAXCODE = TTF.TAXCODE AND TTF.SOURCEDOCUMENTLINE = TT.SOURCEDOCUMENTLINE
WHERE TT.TRANSDATE between @FECHA_INI AND @FECHA
AND TT.TAXORIGIN = 11
GO
现在我已经全部选择进入我的表,现在我想知道如何用最后4行查询实现我的时态表,有人可以帮忙吗?此致
答案 0 :(得分:0)
你可以试试这个。
;WITH TTC AS
(SELECT TT.TAXAMOUNT,TT.TAXCODE,TT.TAXITEMGROUP,TT.VOUCHER,TT.TAXORIGIN, TTF.SOURCERECID
FROM TAXTRANS TT
INNER MERGE JOIN TAXTABLE TAX ON TAX.PAYMENTTAXCODE = TT.TAXCODE AND TAX.DATAAREAID = 'vb'
INNER MERGE JOIN TAXTRANS TTF ON TAX.TAXCODE = TTF.TAXCODE AND TTF.SOURCEDOCUMENTLINE = TT.SOURCEDOCUMENTLINE
WHERE TT.TRANSDATE between @FECHA_INI AND @FECHA
AND TT.TAXORIGIN = 11 AND ("+@Codigo_IEPSCUOTAP_TT+"))
-- MAIN TABLE
-- --JOIN one
LEFT MERGE JOIN
TTC TTIEPSCUOTA ON
C1.TT_VOUCHER = TTIEPSCUOTA.VOUCHER
AND C1.TT_TAXITEMGROUP = TTIEPSCUOTA.TAXITEMGROUP
AND PRO.TTF_SOURCERECID = TTIEPSCUOTA.SOURCERECID
--JOIN two
LEFT MERGE JOIN
TTC TTIEPS25 ON
C1.TT_VOUCHER = TTIEPS25.VOUCHER
AND C1.TT_TAXITEMGROUP = TTIEPS25.TAXITEMGROUP
AND PRO.TTF_SOURCERECID = TTIEPS25.SOURCERECID