我在同一个表中存储了大量select的存储过程但不同的WHERE和不同的INNER JOINS。
我有两种类型的选择:
SELECT TT.VOUCHER,TT.DATAAREAID,ISNULL(LJT.VENDTRANSID,0) AS LJT,
SUM(IIF(TT.TAXITEMGROUP = 'ANTICIPOS',-1*TAXBASEAMOUNT,TAXBASEAMOUNT))AS TAXBASEAMOUNT,
SUM(IIF(TT.TAXITEMGROUP = 'ANTICIPOS',-1*TAXAMOUNT,TAXAMOUNT)) AS TAXAMOUNT
FROM TAXTRANS TT
WHERE... INNER JOIN...
SELECT OFFSETTRANSVOUCHER, offsetrecid, cs.accountnum, TT.DATAAREAID,
SUM(IIF(TAXBASEAMOUNT<=0,TAXBASEAMOUNT,0)) AS TAXBASEAMOUNTNEG,
SUM(IIF(TAXBASEAMOUNT>0,TAXBASEAMOUNT,0)) AS TAXBASEAMOUNTPOS,
SUM(IIF(TAXAMOUNT<=0,TAXAMOUNT,0)) AS TAXAMOUNTNEG,
SUM(IIF(TAXAMOUNT>0,TAXAMOUNT,0)) AS TAXAMOUNTPOS
FROM TAXTRANS TT
WHERE... INNER JOIN...
他们从TAXTRANS TT
检索数据,并且像我之前说的那样重复了很多次。如何只对这两个查询进行一次选择,只进行一次调用,并将其用于不同的WHERE
和INNERJOINS
是否可能?
更新:
我创建临时表作为下面的评论,如:
IF OBJECT_ID('tempdb..##TGJAE' ) IS NOT NULL DROP TABLE ##TGJAE
GO
SELECT
DATAAREAID,
TAXITEMGROUP,
TAXBASEAMOUNT,
VOUCHER,
SOURCERECID
INTO ##TGJAE
FROM TAXTRANS
GO
如何用它替换我的选择?只需更改FROM
并从select中删除TT.
,如下所示:
SELECT VOUCHER,DATAAREAID,ISNULL(LJT.VENDTRANSID,0) AS LJT,
SUM(IIF(TAXITEMGROUP = 'ANTICIPOS',-1*TAXBASEAMOUNT,TAXBASEAMOUNT))AS TAXBASEAMOUNT,
SUM(IIF(TT.TAXITEMGROUP = 'ANTICIPOS',-1*TAXAMOUNT,TAXAMOUNT)) AS TAXAMOUNT
FROM ##TGJAE
此致