行变列,查找某一时刻附近记录

SELECT ccc.realsoetime,ccc.mac, ccc.ControlTime , sum  (case ccc.soetype when 5 then 1 else 0 end )  as '零序过流跳闸' ,    
   sum  (case ccc.soetype when 128 then 1 else 0 end )  as '开关合到分' ,   sum  (case ccc.soetype when 133 then 1 else 0 end )  as '绝缘试验',     
      max  (case ccc.soetype when 128 then ccc.soetime else 0 end )  as '开关合到分时间' ,   
        max  (case ccc.soetype when 5 then ccc.soetime else 0 end )  as '零序过流跳闸时间'  ,
        (case  when  ccc.ControlTime IS NULL   then '就地' else '远程' end )  as '操作方式'    
           from (       select [Soe_Record].*,aaa.realsoetime ,RemoteControlTryRecord.ControlTime from (      
               SELECT [soetime] as realsoetime ,[Soe_Record].soetype,mac   
                FROM [Soe_Record] where  soetype=133 and  mac='015220090804'  and   
                  soetime between  '2022-08-16 00:00:00' and '2022-08-27 00:00:00'   ) as aaa left join [Soe_Record]
                   on  (DATEDIFF(SECOND,aaa.realsoetime,[Soe_Record].soetime) BETWEEN -1 AND 1) and  [Soe_Record].mac=aaa.mac 
                   and [Soe_Record].soetime between  '2022-08-16 00:00:00' and '2022-08-27 00:00:00'
                   left join  RemoteControlTryRecord on  RemoteControlTryRecord.MACID=aaa.mac 
                    and (DATEDIFF(SECOND,aaa.realsoetime,RemoteControlTryRecord.ControlTime) BETWEEN -10 AND 10)    )  as ccc GROUP BY ccc.realsoetime,ccc.mac, ccc.ControlTime 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值