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

日付間隔で複数の列をグループ化して取得

    私には、次のようになります:

    select 
      sum(case when b.materialid = 1 and
                    to_number(to_char(b.scale_eventdate, 'dd')) between  1 and 15 then 
                    b.scale_weight 
          end) mtrl1,
      --      
      sum(case when b.materialid = 2 and
                    to_number(to_char(b.scale_eventdate, 'dd')) between 16 and 31 then 
                    b.scale_weight 
          end) mtrl2
    from datalog_tab b
    where to_char(b.scale_eventdate, 'yyyymm') = '202010' 
      
    

    つまり、scale_eventdateの日かどうかを確認します 列は月の前半または後半に属し、scale_weightを合計します それに応じて。



    1. BrentOzarとPinalDaveによるSQLServerのパフォーマンスに関するアドバイス

    2. MariaDBでのROUND()のしくみ

    3. SQL、テーブル構造を更新する方法

    4. 致命的なエラー:未定義の関数mysql_connect()の呼び出し