【问题标题】:sql coalesce chenge to select casesql coalesce chenge 选择案例
【发布时间】:2017-08-29 09:15:32
【问题描述】:

我有 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

但我想对该代码使用选择和大小写

【问题讨论】:

  • 您使用的是 MySQL 还是 MS SQL Server?不要标记未涉及的产品。
  • 添加示例表数据和预期结果 - 以及格式化文本。
  • @jarlh 嗨,你帮我一次 tnx 因为这里是示例数据stackoverflow.com/questions/43162761/…

标签: mysql sql sql-server select case


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2022-01-20
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多