SQL

以前在一张 表里面显示的 S1 S2 S3 三个字段, 现在拆分到了 一个表里面的 3 条记录。 

 

为了兼容之前的版本, 同事写出视图采用 ORL CASE WHEN 。。。 END 的结构:

 

select  case when ts.s_order='1' then ts.s_date else null END  date1,
          case when ts.s_order='2' then ts.s_date else null END  date2,
          case when ts.s_order='3' then ts.s_date else null END  date3,
           case when ts.s_order='1' then  ts.pilot_id else null END  pilot1,
            case when ts.s_order='2' then  ts.pilot_id else null END pilot2,
              case when ts.s_order='3' then  ts.pilot_id else null END pilot3,
             case when ts.s_order='1' then  ts.s_no else null END  s_no1,
            case when ts.s_order='2' then  ts.s_no else null END  s_no2,
              case when ts.s_order='3' then  ts.s_no else null END  s_no3,
       ts.suite_id
      from sim_training_sim ts
      
      WHERE ts.suite_id = 105698

 

 

 

查询出的结果:

 



 

 

 

然后,再将这些结果合并成一条记录, 就符合上一期的表结构要求了

 

select  max(case when ts.s_order='1' then ts.s_date else null end)  date1,
          max(case when ts.s_order='2' then ts.s_date else null end)  date2,
          max(case when ts.s_order='3' then ts.s_date else null end)  date3,
           max(case when ts.s_order='1' then  ts.pilot_id else null end)  pilot1,
            max(case when ts.s_order='2' then  ts.pilot_id else null end ) pilot2,
             max( case when ts.s_order='3' then  ts.pilot_id else null end ) pilot3,
             max(case when ts.s_order='1' then  ts.s_no else null end)  s_no1,
            max(case when ts.s_order='2' then  ts.s_no else null end)  s_no2,
             max( case when ts.s_order='3' then  ts.s_no else null end)  s_no3,
       ts.suite_id
      from sim_training_sim ts
      group by  ts.suite_id
      

 

 

使用了  GROUP BY   来将结果合并, 但是查询的字段 必须是在函数中的, 所以使用 MAX 函数构造 结果: 

 




 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值