来自不同列和不同表的总和,并在每年的一行中显示结果

时间:2019-05-15 05:57:20

标签: mysql sum

我有两个名为table1table2的表。我尝试对某些列求和,但结果显示错误

我尝试了以下mysql查询:

SELECT t1.year
     , SUM(t1.deposit) TOTALDEPOSIT
     , SUM(t1.interest) TOTALINTEREST 
     , SUM(t1.otherinterest) TOTALOTHER
  FROM table1 t1
  LEFT 
  JOIN table2 t2
    ON t1.year = t2.year
 GROUP 
    BY t1.year

但是SUM的结果显示不正确

我的桌子在下面

table1
| table1id| year| deposit| interest|
|---------|-----|--------|---------|
|        1|2019 |     20 |       1 |
|        2|2019 |     20 |       2 |
|        3|2019 |     20 |       1 |
|        3|2019 |     20 |       2 |
|        3|2020 |     20 |       3 |
|        3|2020 |     20 |       4 |

table2
| table2id| year | otherinterest|  
|----------------|--------------| 
|       1 | 2019 |        10    |       
|       2 | 2019 |        10    |  

预期结果是

| YEAR | TOTALDEPOSIT| TOTALINTEREST  |TOTALOTHER |
|--------------------|----------------|-----------| 
| 2019 |       120   |        6       |     20    |
| 2020 |       40    |        7       |           |

但是我的查询给出了结果

| YEAR | TOTALDEPOSIT| TOTALINTEREST  |TOTALOTHER |
|--------------------|----------------|-----------| 
| 2019 |       160   |        12      |     80    |
| 2020 |       40    |        7       |           |

那么您能不能请任何人帮助我解决此查询?

5 个答案:

答案 0 :(得分:3)

您的查询无法正常运行,因为中间结果可能与您预期的不同。 让我们尝试以下查询:

SELECT *
  FROM table1 t1
LEFT JOIN table2 t2
  ON t1.year = t2.year

结果将是:

+----------+------+---------+----------+----------+------+---------------+
| table1id | year | deposit | interest | table2id | year | otherinterest |
+----------+------+---------+----------+----------+------+---------------+
|        1 | 2019 |      20 |        1 |        1 | 2019 |            10 |
|        2 | 2019 |      20 |        2 |        1 | 2019 |            10 |
|        3 | 2019 |      20 |        1 |        1 | 2019 |            10 |
|        3 | 2019 |      20 |        2 |        1 | 2019 |            10 |
|        1 | 2019 |      20 |        1 |        2 | 2019 |            10 |
|        2 | 2019 |      20 |        2 |        2 | 2019 |            10 |
|        3 | 2019 |      20 |        1 |        2 | 2019 |            10 |
|        3 | 2019 |      20 |        2 |        2 | 2019 |            10 |
|        3 | 2020 |      20 |        3 |     NULL | NULL |          NULL |
|        3 | 2020 |      20 |        4 |     NULL | NULL |          NULL |
+----------+------+---------+----------+----------+------+---------------+

因此,我们有10行,而不是6行。您可以看到例如2019年的存款总额为160。与您的“错误”结果中的数字相同。

这是因为对于表1中年份为2019加入条件(t1.year = t2.year)的每个记录都是两次。 换句话说,对于表1中等于年2019的那一行,结果表中有两行-其中一行的table2id = 1,另一行的table2id = 2。

答案 1 :(得分:2)

子查询比联接少了一些麻烦。

drop table if exists t,t1;

create table t
(table1id int, year int, deposit int, interest int);
insert into t values
(        1,2019 ,     20 ,       1), 
(        2,2019 ,     20 ,       2), 
(        3,2019 ,     20 ,       1), 
(        3,2019 ,     20 ,       2), 
(        3,2020 ,     20 ,       3), 
(        3,2020 ,     20 ,       4); 

create table t1
( table2id int, year int, otherinterest int);  
insert into t1 values
(       1 , 2019 ,        10    ),       
(       2 , 2019 ,        10    );

select t.year,sum(deposit),sum(interest),
                (select sum(otherinterest) from t1 where t1.year = t.year) otherinterest
FROM t
group by t.year;



+------+--------------+---------------+---------------+
| year | sum(deposit) | sum(interest) | otherinterest |
+------+--------------+---------------+---------------+
| 2019 |           80 |             6 |            20 |
| 2020 |           40 |             7 |          NULL |
+------+--------------+---------------+---------------+
2 rows in set (0.00 sec)

答案 2 :(得分:1)

只需使用一个简单的子查询,它将起作用。

SELECT A.year, SUM(A.deposit) TOTALDEPOSIT, SUM(A.interest) TOTALINTEREST, 
    (SELECT SUM(B.otherinterest) FROM table2 B WHERE B.year= A.year) TOTALOTHER
FROM table1 A 
GROUP BY A.year

答案 3 :(得分:0)

您应该将每个表的合并结果加入其中,例如:

select  t1.year, tt1.totaldeposit, tt1.totalinterest, tt2.otherinterest
from   table1 t1
inner join  (
  select year, sum(deposit) totaldeposit, sum(interest) totalinterest
  from table1 
  group by year
) tt1 On t1.year = tt1.year
left join  (
   select year, sum(otherinterest) otherinterest
  from table1 
  group by year
) tt2 On t1.year = tt2.year

答案 4 :(得分:0)

您需要使用带有GROUP BY的简单LEFT JOIN,但是操作顺序应与您的操作顺序不同:)

select t1.year, 
       t1.deposit totaldeposit, 
       t1.interest totalinterest, 
       t2.otherinterest * t1.cnt totalother
from (
    select year, sum(deposit) deposit, sum(interest) interest, count(*) cnt
    from table1
    group by year
) t1 left join (
    select year, sum(otherinterest) otherinterest
    from table2
    group by year
) t2 on t1.year = t2.year