简体   繁体   中英

SQL how to sum two rows from the same colum

桌子

The picture shows the table

and my question calculate and show lily total fees ( teacher + assistant)

  • year 2014 - teacher id (123), assistant id (142)
  • year 2015 - teacher id (523), assistant id (124)

Have you tried a group by?

SELECT
    year,
    SUM(charge) charge
FROM
    your_table
GROUP BY
    year
ORDER BY
    year;

The structure of the table is not really good, for example the year data is missing.

But you can run the following SQL statement which uses union all:

select '2014' as year,
       sum(charge)
  from your_table
 where id in (123, 142)

union all

select '2015' as year,
       sum(charge)
  from your_table
 where id in (523, 124);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM