我正在使用SQL Server2014。我有一个税率表,其中包含税率的有效日期。没有指定结束日期,只有生效日期。每个税率可能具有多个税法管辖区,必须对这些税率进行累加才能得出有效税率。该查询用于在指定日期输出系统中的所有代码和税率:
SELECT
Tx.STCCode,
SUM(Tx.tax) AS 'Rate'
FROM
(SELECT
TT.STCCode,
(SELECT TOP 1 TR.Rate
FROM STA1 TR
WHERE TT.STACode = TR.StaCode
AND TT.STAType = TR.SttType
AND '10/31/2017' >= TR.EfctDate
ORDER BY TR.EfctDate DESC) AS Tax
FROM
STC1 TT) AS Tx
GROUP BY
Tx.STCCode
ORDER BY
Tx.STCCode
输出:
STCCode Rate
--------------------
CT 6.350000
EX 0.000000
MA 6.250000
NJ 6.875000
NYC 8.875000
NYNAS 8.625000
NYNR 8.375000
NYWC 7.375000
NYWP 8.375000
PA 6.000000
PHIL 8.141500
PHILBP 0.141500
RI 7.000000
SOFT 1.000000
我还有另一个记录表,其中包括交易日期和交易时需要计算税率的税码。如果我尝试将上面的查询用作税率字段的子查询,那么我将无法获得需要的字段到上面的查询的where子句中。很有可能是由于上述示例中的子查询。
我主要是一个SQL hack,一直在实时学习它。谢谢您的帮助。
带有子查询的样本。我需要字段T1.U_DTSIGNED来替换tax子查询中的硬编码日期:
select
T1.U_DTSIGNED,
T.U_SUBJOBID,
T1.U_COID,
T.U_TAXCODE,
(select
SUM(Tx.tax)
FROM
(Select TT.STCCode,
(Select TOP 1
TR.Rate
FROM STA1 TR
WHERE TT.STACode = TR.StaCode and TT.STAType = TR.SttType and
'10/31/2017' >=TR.EfctDate
Order By TR.EfctDate DESC
) as Tax
FROM STC1 TT ) as Tx
WHERE T.U_TAXCODE = Tx.STCCode
GROUP BY Tx.STCCode) as 'Rate'
FROM [@NWRCHGORDLINE] T
INNER JOIN [@NWRCHGORDHEADER] T1 ON T.U_COID = T1.U_COID and T.U_JOBID =
T1.U_JOBID
WHERE T.U_ISTotal = 'Y' and T1.U_JOBID = 17106
Order By Len(T.U_COID),T.U_COID
示例结果:
2017-12-28 00:00:00.000,171060001,31,NJ,6.875000
2017-12-28 00:00:00.000,171060002,31,EX,0.000000
2017-12-28 00:00:00.000,171060003,31,EX,0.000000
2017-12-28 00:00:00.000,171060004,31,NJ,6.875000
2018-01-03 00:00:00.000,171060001,32,NJ,6.875000
2018-01-03 00:00:00.000,171060002,32,EX,0.000000
2018-01-03 00:00:00.000,171060003,32,EX,0.000000
2018-01-03 00:00:00.000,171060004,32,NJ,6.875000
所需结果,因为如果DTSigned日期为2018年,新泽西州的税率将在2018年1月1日生效,因此应显示更改的税率:
2017-12-28 00:00:00.000,171060001,31,NJ,6.875000
2017-12-28 00:00:00.000,171060002,31,EX,0.000000
2017-12-28 00:00:00.000,171060003,31,EX,0.000000
2017-12-28 00:00:00.000,171060004,31,NJ,6.875000
2018-01-03 00:00:00.000,171060001,32,NJ,6.625000
2018-01-03 00:00:00.000,171060002,32,EX,0.000000
2018-01-03 00:00:00.000,171060003,32,EX,0.000000
2018-01-03 00:00:00.000,171060004,32,NJ,6.625000
答案 0 :(得分:0)
希望这个CROSS APPLY
可以满足您的要求:
;with STC1 as (
select STACode = 'CT', STAType ='',STCCode='CT'
union all
select STACode = 'NJ', STAType ='',STCCode='NJ'
), STA1 as (
select Rate = 6.875, STAType ='',EfctDate='2017-01-01',StaCode ='NJ',SttType=''
union all
select Rate = 6.625, STAType ='',EfctDate='2018-01-01',StaCode ='NJ',SttType=''
union all
select Rate = 6.35, STAType ='',EfctDate='2017-01-01',StaCode ='CT',SttType=''
),[@NWRCHGORDLINE] as
(
select U_COID='',U_JOBID='17106',U_ISTotal='Y',U_SUBJOBID='',U_TAXCODE='NJ'
union all
select U_COID='',U_JOBID='17106',U_ISTotal='Y',U_SUBJOBID='',U_TAXCODE='CT'
),[@NWRCHGORDHEADER] as
(
select U_COID='',U_JOBID='17106',U_DTSIGNED='2017-12-31',U_TAXCODE='NJ'
union all
select U_COID='',U_JOBID='17106',U_DTSIGNED='2018-12-31',U_TAXCODE='NJ'
)
select
T1.U_DTSIGNED,
T.U_SUBJOBID,
T1.U_COID,
T.U_TAXCODE,
Tax.Rate
FROM [@NWRCHGORDLINE] T
INNER JOIN [@NWRCHGORDHEADER] T1
ON T.U_COID = T1.U_COID
and T.U_JOBID = T1.U_JOBID
cross apply ((select SUM(Tx.tax)
from (Select TT.STCCode,
(Select TOP 1
TR.Rate
FROM STA1 TR
WHERE TT.STACode = TR.StaCode
and TT.STAType = TR.SttType
and T1.U_DTSIGNED >= TR.EfctDate
Order By TR.EfctDate DESC
) as Tax
FROM STC1 TT ) as Tx
WHERE T.U_TAXCODE = Tx.STCCode
GROUP BY Tx.STCCode
)) Tax (Rate)
WHERE T.U_ISTotal = 'Y'
and T1.U_JOBID = 17106
Order By Len(T.U_COID),T.U_COID