向视图添加额外的计算列,该列不存在于表中

时间:2019-06-19 15:43:14

标签: sql view

我想用三个表的联合创建视图。但是在结果查询中,我想要多一列。

我的代码就像

CREATE OR REPLACE VIEW DISCOUNT
AS SELECT 
* FROM
(
SELECT 
    T1.ID,
    T1.AMOUNT,
    T1.ITEM,
    T1.DATA,
    T1.DISC_PERCENT,
    T4.UOM AS(SELECT UomCode 
                     FROM T4
                     JOIN Table5 T5 ON T4.UomEntry = T5.UomEntry
                     JOIN Table6 T6 on T6.UgpEntry = T4.UgpEntry
                     where T6.ITEM = T1.ITEM
                     and T5.UomEntry = T6.PriceUnit)
FROM Table1 T1,Table4 T4
UNION ALL
SELECT 
    T2.ID,
    T2.AMOUNT,
    T2.ITEM,
    T2.DATA,
    T2.DISC_PERCENT,
    T4.UOM AS(SELECT UomCode 
                     FROM T4
                     JOIN Table5 T5 ON T5.UomEntry = T4.UomEntry
                     JOIN Table6 T6 on T6.UgpEntry = T4.UgpEntry
                     where T6.ITEM = T1.ITEM
                     and T5.UomEntry = T6.PriceUnit)
FROM Table2 T2
UNION ALL
SELECT
    T3.ID,
    T3.BILLAMOUNT,
    T3.DATA,
    T3.BUSINESSCODE,
    T3.DISC_PERCENT  
FROM Table3 T3
);

因此,在我拥有选择嵌套sql管理的点上,已经向我发出错误信号,我不知道如何解决

1 个答案:

答案 0 :(得分:0)

您只需投影一个空值即可使所有组具有相同的列数,如下所示

CREATE OR REPLACE VIEW DISCOUNT
AS SELECT 
* FROM
(
SELECT 
    T1.ID,
    T1.AMOUNT,
    T1.ITEM,
    T1.DATA,
    T1.DISC_PERCENT,
    T4.UOM AS(SELECT UomCode 
                     FROM T4
                     JOIN Table5 T5 ON T4.UomEntry = T5.UomEntry
                     JOIN Table6 T6 on T6.UgpEntry = T4.UgpEntry
                     where T6.ITEM = T1.ITEM
                     and T5.UomEntry = T6.PriceUnit)
FROM Table1 T1,Table4 T4
UNION ALL
SELECT 
    T2.ID,
    T2.AMOUNT,
    T2.ITEM,
    T2.DATA,
    T2.DISC_PERCENT,
    T4.UOM AS(SELECT UomCode 
                     FROM T4
                     JOIN Table5 T5 ON T5.UomEntry = T4.UomEntry
                     JOIN Table6 T6 on T6.UgpEntry = T4.UgpEntry
                     where T6.ITEM = T1.ITEM
                     and T5.UomEntry = T6.PriceUnit)
FROM Table2 T2
UNION ALL
SELECT
    T3.ID,
    T3.BILLAMOUNT,
    T3.DATA,
    T3.BUSINESSCODE,
    T3.DISC_PERCENT  
     Null AS UOM
FROM Table3 T3
) ;