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

PostgreSQLの複数の数式

    SQLFiddle

    select
      2.5*((r-i)/(r+(6*i)-(7.5*n)+1)) as tera,
      (r-(2*i)-n)/(r+(2*i)-n) as tera2
    from
      (
      select
        sum(case when wavelength between 340 and 345 then reflectance end) as r,
        sum(case when wavelength between 350 and 355 then reflectance end) as i,
        sum(case when wavelength between 360 and 365 then reflectance end) as n
      from
        test
      ) vars
    

    複数のテーブルの場合、次のようなものを使用できます:

    select
      table_name,
      2.5*((r-i)/(r+(6*i)-(7.5*n)+1)) as tera,
      (r-(2*i)-n)/(r+(2*i)-n) as tera2
    from
      (
      select
        table_name,
        sum(case when wavelength between 340 and 345 then reflectance end) as r,
        sum(case when wavelength between 350 and 355 then reflectance end) as i,
        sum(case when wavelength between 360 and 365 then reflectance end) as n
      from
        (
          select 'table 1' as table_name, * from test
          union all
          select 'table 2', * from test
          union all
          select 'table 3', * from test      
          union all
          select 'table 4', * from test  
        ) as all_tables
      group by
        table_name
      ) vars
    

    SQLFiddle

    テーブル名を実際の名前に変更し、union allを繰り返すだけです。 必要な回数だけ。



    1. PL/SQLの単体テスト

    2. Oracleの日付にAD/BCインジケーターを追加する方法

    3. サブクエリから複数の値を取得することは可能ですか?

    4. REGEXP_REPLACE-()で囲まれている場合にのみ、文字列から改行を削除します\ n