在 Oracle SQL Developer 中合并表并使用表名称创建新的自定义列

问题描述 投票:0回答:1

我有大约十个表,它们具有相同的列维度和相同的列名。这些表与

UNION ALL
结合在一起。 现在我想创建一个自定义列并将表的名称添加到所有这些不同的表中。

SELECT TO_CHAR(begtime, 'YYYY-MM') AS "DelivMonth",
       ROUND(SUM(quantity), 2) AS "Quantity",
       ROUND(SUM(value) , 2) AS "ContractValue",
       product AS "Product",
       intention AS "Intention"
FROM table1
GROUP BY TO_CHAR(begtime, 'YYYY-MM'), product, kelag_intention

UNION ALL 

SELECT TO_CHAR(begtime, 'YYYY-MM') AS "DelivMonth",
       ROUND(SUM(quantity), 2) AS "Quantity",
       ROUND(SUM(value) , 2) AS "ContractValue",
       product AS "Product",
       intention AS "Intention"
FROM table2
GROUP BY TO_CHAR(begtime, 'YYYY-MM'), product, kelag_intention

我想要一个名为“Branch”的新列,我可以在其中为每个表创建一个特定条目,例如对于 table1,

Branch
的行条目应该是 Optimization Sales”,对于 table2 它应该是“Validation Sales” 。 我该怎么做?

sql oracle union calculated-columns union-all
1个回答
0
投票

将其添加为新列:

SELECT 'Optimization Sales' AS "Branch",                  --> this
       TO_CHAR(begtime, 'YYYY-MM') AS "DelivMonth",
       ROUND(SUM(quantity), 2) AS "Quantity",
       ROUND(SUM(value) , 2) AS "ContractValue",
       product AS "Product",
       intention AS "Intention"
FROM table1
GROUP BY TO_CHAR(begtime, 'YYYY-MM'), product, kelag_intention

UNION ALL 

SELECT 'Validation Sales' AS "Branch"                     --> this
       TO_CHAR(begtime, 'YYYY-MM') AS "DelivMonth",
       ROUND(SUM(quantity), 2) AS "Quantity",
       ROUND(SUM(value) , 2) AS "ContractValue",
       product AS "Product",
       intention AS "Intention"
FROM table2
GROUP BY TO_CHAR(begtime, 'YYYY-MM'), product, kelag_intention
© www.soinside.com 2019 - 2024. All rights reserved.