2017-10-05 37 views
0

加入我有這樣一個查詢:重構與時態表

-- --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 ("[email protected]_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 ("[email protected]_IEPS25P_TT+")) TTIEPS25 ON 
C1.TT_VOUCHER = TTIEPS25.VOUCHER 
AND C1.TT_TAXITEMGROUP = TTIEPS25.TAXITEMGROUP 
AND PRO.TTF_SOURCERECID = TTIEPS25.SOURCERECID 

正如你可以看到Join oneJoin two是除了最後4行相同的連接,所以我決定創建一個臨時表來調用只選擇一次,如:

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

sql server 2012,但我作爲OLEDB任務在SSIS項目上實現,我做臨時表是因爲這兩個使用相同的select,並且select有500多萬個寄存器,所以我只想調用一次保存到臨時表中,現在我想做的和我在原創一樣,但使用臨時表而不是選擇,因爲我的查詢的前六行是我的臨時表@ DanielGimenez – Benny

+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 ("[email protected]_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 
+0

和沒有CTE,我們可以調用臨時表或不?沒有cte的 – Benny

+0

,你沒有任何其他的選擇。 –

+0

我想我可以使用'(SELECT * FROM ## TTC WHERE(「+ @ Codigo_IEPSCUOTAP_TT +」))TTIEPSCUOTA ON' ...等 – Benny