sql coalesce chenge来选择case

时间:2017-04-04 09:03:22

标签: mysql sql sql-server select case

我有2个表,希望从条件中得到2个表的行和 结果就像这样,例如sh2:19 + 5 + 11 + 5 = 40每个名称在表中计算它们 我使用那段代码

select name, sum(total) total from 
(select name, sum(coalesce(sk,ss)) total
 from table1
 group by name
 union all
 select name, sum(coalesce(sk,ss)) total
 from table2
 group by name
) t
group by name

但我希望对该代码使用select和case

1 个答案:

答案 0 :(得分:0)

select name,SUM(total)total from(
      select name,sum(case when sk>0 then sk else ss end )total from majale group by name
      union all
      select name,sum(case when sk>0 then sk else ss end )from ketab group by name
      union all
      select name,sum(case when sk>0 then sk else ss end )from savabegh group by name
      )t
      group by name