我正在为我的一门课做作业,并在高低处搜索了我的问题的答案,但找不到答案。我有两张正在使用的桌子。
他们是:
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 |
+-----------+
但我需要它来匹配他上面给出的例子。
任何帮助将不胜感激。我很确定这完全是因为我没有选择正确的日期来确定要使用的TaxAuthority和AuthTaxRate。再次感谢所有帮助。