sql >> データベース >  >> RDS >> Mysql

MySQLは、オンとオフのタイムスタンプと3秒の間で最大値を選択します

    以下のクエリは、ステータスがnullではないすべての行を選択します。

    ステータスが「オフ」の行の場合、サブクエリを使用して、最新の「オン」と現在の「オフ」+3秒の間の最大の重みを返します

    select t.TimeStr, t.Status,
      case
        when status = 'Off'
        then (select max(cast(Weight as signed)) from tempbatch t2 
              where t2.TimeStr between 
                (select max(TimeStr) 
                     from tempbatch t3 
                     where status = 'On' and t3.TimeStr < t.TimeStr)
              and date_add(t.TimeStr, interval 3 second)
        )
        else Weight
      end as Weight
    from tempbatch t
    where t.Status is not null
    order by TimeStr
    

    http://sqlfiddle.com/#!9/f27fb/6



    1. Eclipse-新しいMariaDB接続を追加する方法

    2. FORJSONPATHを使用してネストされたJSON配列を作成します

    3. EntityFrameworkがテンポラルテーブルで機能しない

    4. Laravel:データベースに動的に接続する