首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL高级求和查询

问MySQL高级求和查询
EN

Stack Overflow用户
提问于 2013-11-10 09:49:47
回答 1查看 168关注 0票数 0

我有一个表,看起来像这样:

代码语言:javascript
复制
+--------------+------------+-------------+
| 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的税率。示例:

代码语言:javascript
复制
City2 = 2.0
County1=2.5
State1=1.1
Total=5.6

到目前为止,我已经得到了所有的总数加起来是5.6;但是,我不知道如何将这3个和相加。这是我到目前为止所做的陈述:

代码语言:javascript
复制
    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';

我的声明的结果是:

代码语言:javascript
复制
+-------------+
| AuthTaxRate |
+-------------+
|         2.0 |
|         2.5 |
|         1.1 |
+-------------+

有人知道如何将这些值相加吗?

EN

回答 1

Stack Overflow用户

发布于 2013-11-10 10:06:30

我现在确定了,但你可以这样尝试-

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19885363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档