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

后端 未结 5 724
故里飘歌
故里飘歌 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条回答
  •  -上瘾入骨i
    2020-11-28 08:21

    In SQL Server

    You can do this using With CTE

    WITH common_table_expression (Transact-SQL)

    CREATE TABLE tab(ColumnA DECIMAL(10,2), ColumnB DECIMAL(10,2), ColumnC DECIMAL(10,2))
    
    INSERT INTO tab(ColumnA, ColumnB, ColumnC) VALUES (2, 10, 2),(3, 15, 6),(7, 14, 3)
    
    WITH tab_CTE (ColumnA, ColumnB, ColumnC,calccolumn1)  
    AS  
    (  
    Select
        ColumnA,
        ColumnB,
        ColumnC,
        ColumnA + ColumnB As calccolumn1
      from tab
    )  
    
    SELECT
      ColumnA,
      ColumnB,
      calccolumn1,
      calccolumn1 / ColumnC AS calccolumn2
    FROM  tab_CTE
    

    DBFiddle Demo

提交回复
热议问题