简体   繁体   English

sql合并chenge选择大小写

[英]sql coalesce chenge to select case

I have 2 tables and want to get the row sum from 2 tables with conditions the result to be like this for example sh2 : 19 + 5 + 11 + 5 = 40 for each name calculate them all in a table i use that code 我有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

but i want use select and case for that code 但我想使用select和case的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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