SQL从税表中获取不同的税率

时间:2017-05-02 14:13:56

标签: sql sql-server

我乐观地认为,一些聪明的SQL作者可以调整我的查询以提供我正在寻找的结果。我必须提取州和不同税种的当前税率(PST / QST / GST / HST)每州通常有PST / GST或HST。查看下面的数据表,我需要获得“短名称”,“名称”和“生效日期”的不同值的税率“。”

所需的结果是此表:

EffectiveDate  Rate ShortName   name
2016-01-01  0.050000    AB  GST
2013-04-01  0.050000    BC  GST
2013-04-01  0.070000    BC  PST
2013-07-01  0.050000    MB  GST
2013-07-01  0.080000    MB  PST
2016-07-01  0.150000    NB  HST
2016-07-01  0.150000    NL  HST
2016-01-01  0.150000    NS  HST
2016-01-01  0.050000    NT  GST
2016-01-01  0.050000    NU  GST
2015-04-01  0.130000    ON  HST
2016-10-01  0.150000    PE  HST
2013-01-01  0.050000    QC  GST
2017-03-01  0.099750    QC  QST
2016-01-01  0.050000    SK  GST
2017-03-01  0.060000    SK  PST
2009-02-01  0.050000    YT  GST

到目前为止我的查询:

    SELECT --TaxRates.ID, --TaxRates.StateId, TaxRateDetails.id, 
TaxRateDetails.EffectiveDate,  TaxRateDetails.Rate,--TaxRateDetails.TaxRateId,
States.ShortName,   TaxImpositionTypes.name -- DISTINCT Name,states.ShortName, TaxRateDetails.Id,TaxImpositionTypeDetailId 
FROM dbo.TaxRates
join dbo.TaxRateDetails ON TaxRateDetails.TaxRateId = TaxRates.Id
INNER JOIN States ON dbo.TaxRates.StateId = States.Id  AND States.IsActive =1
INNER JOIN TaxImpositionTypeDetails ON TaxRateDetails.TaxImpositionTypeDetailId = TaxImpositionTypeDetails.Id 
INNER JOIN TaxImpositionTypes ON dbo.TaxImpositionTypeDetails.TaxImpositionTypeId = TaxImpositionTypes.Id
WHERE  EffectiveDate < CURRENT_TIMESTAMP
order BY ShortName

此查询创建的源表如下:

EffectiveDate  Rate    ShortName    name
2016-01-01  0.050000    AB  GST
2009-02-01  0.050000    AB  GST
2013-04-01  0.050000    BC  GST
2013-04-01  0.070000    BC  PST
2013-04-01  0.050000    BC  GST
2013-04-01  0.070000    BC  PST
2013-07-01  0.050000    MB  GST
2013-07-01  0.080000    MB  PST
2010-07-01  0.130000    NB  HST
2016-07-01  0.150000    NB  HST
2010-07-01  0.130000    NB  HST
2009-02-01  13.000000   NL  HST
2016-07-01  0.150000    NL  HST
2010-07-01  0.130000    NL  HST
2016-01-01  0.150000    NS  HST
2010-07-01  0.150000    NS  HST
2016-01-01  0.050000    NT  GST
2009-02-01  0.050000    NT  GST
2016-01-01  0.050000    NU  GST
2009-02-01  0.050000    NU  GST
2010-07-01  0.130000    ON  HST
2009-02-01  0.050000    ON  HST
2009-02-01  0.080000    ON  PST
2010-03-01  0.130000    ON  HST
2015-04-01  0.130000    ON  HST
2015-04-01  0.000100    ON  PST
2009-02-01  13.000000   PE  HST
2013-04-01  0.140000    PE  HST
2016-10-01  0.150000    PE  HST
2013-01-01  0.050000    QC  GST
2013-01-01  0.010000    QC  QST
2012-01-01  0.050000    QC  GST
2012-01-01  0.095000    QC  QST
2017-03-01  0.099750    QC  QST
2016-01-01  0.050000    SK  GST
2016-01-01  0.050000    SK  PST
2009-02-01  0.050000    SK  GST
2009-02-01  0.050000    SK  PST
2017-03-01  0.060000    SK  PST
2009-02-01  0.050000    YT  GST

1 个答案:

答案 0 :(得分:3)

如果我正确理解了这个问题,我们可以为每个唯一的短名称和名称顺序配对分配一个行号,按照生效日期降序配对,以便最高日期是第一个。然后我们只显示每个唯一的短名称和名称配对的第1行。

WITH CTE AS (
   SELECT TaxRateDetails.EffectiveDate
        , TaxRateDetails.Rate
        , States.ShortName
        , TaxImpositionTypes.name 
        , Row_number() over (partition by States.ShortName,TaxImpositionTypes.name ORDER BY TaxRateDetails.EffectiveDate DESC) RN
FROM dbo.TaxRates
INNER JOIN dbo.TaxRateDetails 
   ON TaxRateDetails.TaxRateId = TaxRates.Id
INNER JOIN States 
   ON dbo.TaxRates.StateId = States.Id  
  AND States.IsActive =1
INNER JOIN TaxImpositionTypeDetails 
   ON TaxRateDetails.TaxImpositionTypeDetailId = TaxImpositionTypeDetails.Id 
INNER JOIN TaxImpositionTypes 
   ON dbo.TaxImpositionTypeDetails.TaxImpositionTypeId = TaxImpositionTypes.Id
WHERE EffectiveDate < CURRENT_TIMESTAMP)

SELECT * FROM CTE where RN = 1