0

我正在为我的一门课做作业,并在高低处搜索了我的问题的答案,但找不到答案。我有两张正在使用的桌子。

他们是:

TaxAreaAuthority                TaxRates
+--------------+---------+      +--------------+------------+-------------+
| TaxAuthority | TaxArea |      | TaxAuthority | Effective  | AuthTaxRate |
+--------------+---------+      +--------------+------------+-------------+
| city1        | city1   |      | city1        | 1993-01-01 |         1.0 |
| city2        | city2   |      | city1        | 1994-01-01 |         1.5 |
| city3        | city3   |      | city2        | 1993-09-01 |         1.5 |
| county1      | city1   |      | city2        | 1994-01-01 |         2.0 |
| county1      | city2   |      | city2        | 1995-01-01 |         2.5 |
| county2      | city3   |      | city3        | 1993-01-01 |         1.9 |
| state1       | city1   |      | city3        | 1993-07-01 |         2.3 |
| state1       | city2   |      | county1      | 1993-01-01 |         2.3 |
| state1       | city3   |      | county1      | 1994-10-01 |         2.5 |
+--------------+---------+      | county1      | 1995-01-01 |         2.7 |
                                | county2      | 1993-01-01 |         2.4 |
                                | county2      | 1994-01-01 |         2.7 |
                                | county2      | 1995-01-01 |         2.8 |
                                | state1       | 1993-01-01 |         0.5 |
                                | state1       | 1994-01-01 |         0.8 |
                                | state1       | 1994-07-01 |         0.9 |
                                | state1       | 1994-10-01 |         1.1 |
                                +--------------+------------+-------------+

这是带有示例的主要问题:

1994 年 11 月 1 日 city2 的税率是多少?

城市 2 = 2.0,县 1 = 2.5,州 1 = 1.1

总计 = 5.6

我可以使用以下语句从TaxAreaAuthority中选择县和州:

SELECT TaxAuthority FROM TaxAreaAuthority
    WHERE TaxAuthority LIKE 'county%'
    AND TaxArea='city2';

SELECT TaxAuthority FROM TaxAreaAuthority
    WHERE TaxAuthority LIKE 'state%'
    AND TaxArea='city2';

这就是我卡住的地方。我选择正确日期的代码已关闭,这导致它无法将正确的AuthTaxRate项目添加在一起。

SELECT SUM(AuthTaxRate) as 'Tax_Total' FROM TaxRates
    WHERE EXTRACT(YEAR FROM Effective)='1994'
        AND TaxAuthority=(
            (SELECT TaxAuthority FROM TaxRates
                WHERE TaxAuthority='city2'
                AND EXTRACT(year FROM Effective)='1994')
            OR
            (SELECT TaxAuthority FROM TaxAreaAuthority
                WHERE TaxAuthority LIKE 'county%'
                AND TaxArea='city2')
            OR
            (SELECT TaxAuthority FROM TaxAreaAuthority
                WHERE TaxAuthority LIKE 'state%'
                AND TaxArea='city2')
            )
    ;

它返回这个:

+-----------+
| Tax_Total |
+-----------+
|      11.5 |
+-----------+

但我需要它来匹配他上面给出的例子。

任何帮助将不胜感激。我很确定这完全是因为我没有选择正确的日期来确定要使用的TaxAuthorityAuthTaxRate。再次感谢所有帮助。

4

1 回答 1

1

您想对分组最大值求和。

首先,(TaxAuthority, Effective)通过加入表格并对结果进行分组来找到相关的 对:

SELECT   TaxAuthority, MAX(Effective) Effective
FROM     TaxRates JOIN TaxAreaAuthority USING (TaxAuthority)
WHERE    TaxArea = 'city2' AND Effective <= '1994-11-01'
GROUP BY TaxAuthority

然后将结果连接回TaxRates表:

SELECT SUM(AuthTaxRate)
FROM   TaxRates NATURAL JOIN (
  SELECT   TaxAuthority, MAX(Effective) Effective
  FROM     TaxRates JOIN TaxAreaAuthority USING (TaxAuthority)
  WHERE    TaxArea = 'city2' AND Effective <= '1994-11-01'
  GROUP BY TaxAuthority
) t
于 2013-04-02T10:59:44.070 回答