How to use a calculated column to calculate another column in the same view

后端 未结 5 725
故里飘歌
故里飘歌 2020-11-28 07:32

I am hoping you can help with this question. I am using Oracle SQL (SQL Developer for this view)...

If I have a table with the following columns:

  • Co
5条回答
  •  粉色の甜心
    2020-11-28 08:35

    You could use a nested query:

    Select
      ColumnA,
      ColumnB,
      calccolumn1,
      calccolumn1 / ColumnC as calccolumn2
    From (
      Select
        ColumnA,
        ColumnB,
        ColumnC,
        ColumnA + ColumnB As calccolumn1
      from t42
    );
    

    With a row with values 3, 4, 5 that gives:

       COLUMNA    COLUMNB CALCCOLUMN1 CALCCOLUMN2
    ---------- ---------- ----------- -----------
             3          4           7         1.4
    

    You can also just repeat the first calculation, unless it's really doing something expensive (via a function call, say):

    Select
      ColumnA,
      ColumnB,
      ColumnA + ColumnB As calccolumn1,
      (ColumnA + ColumnB) / ColumnC As calccolumn2
    from t42; 
    
       COLUMNA    COLUMNB CALCCOLUMN1 CALCCOLUMN2
    ---------- ---------- ----------- -----------
             3          4           7         1.4 
    

提交回复
热议问题