我有一个看起来像这样的表:
+--------------+------------+-------------+
| TaxAuthority | Effective | AuthTaxRate |
+--------------+------------+-------------+
| city1 | 1993-01-01 | 1.0 |
| city1 | 1994-01-01 | 1.5 |
| city2 | 1993-09-01 | 1.5 |
| city2 | 1994-01-01 | 2.0 |
| city2 | 1995-01-01 | 2.5 |
| city3 | 1993-01-01 | 1.9 |
| city3 | 1993-07-01 | 2.3 |
| county1 | 1993-01-01 | 2.3 |
| 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的税率。例如:
City2 = 2.0
County1=2.5
State1=1.1
Total=5.6
到目前为止,我已经能够获得加起来为5.6的所有金额;但是,我不知道如何加上3笔钱。这是我到目前为止的陈述:
select AuthTaxRate
from TaxRates
where TaxAuthority = 'City2'
and Effective <= '1994-11-01'
and Effective > '1993-09-01'
union
select AuthTaxRate
from TaxRates
where TaxAuthority = 'County1'
and Effective <= '1994-11-01'
and Effective > '1993-09-01'
union
select AuthTaxRate
from TaxRates
where TaxAuthority = 'State1'
and Effective <= '1994-11-01'
and Effective > '1994-07-01';
我的陈述结果是:
+-------------+
| AuthTaxRate |
+-------------+
| 2.0 |
| 2.5 |
| 1.1 |
+-------------+
有谁知道如何添加这些值?
答案 0 :(得分:0)
我现在肯定,但你可以尝试这样 -
Select sum(
select AuthTaxRate
from TaxRates
where TaxAuthority = 'City2'
and Effective <= '1994-11-01'
and Effective > '1993-09-01'
union
select AuthTaxRate
from TaxRates
where TaxAuthority = 'County1'
and Effective <= '1994-11-01'
and Effective > '1993-09-01'
union
select AuthTaxRate
from TaxRates
where TaxAuthority = 'State1'
and Effective <= '1994-11-01'
and Effective > '1994-07-01';
) As total;