Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何对使用单独查询生成的多个列求和?_Sql_Oracle_Oracle11g - Fatal编程技术网

Sql 如何对使用单独查询生成的多个列求和?

Sql 如何对使用单独查询生成的多个列求和?,sql,oracle,oracle11g,Sql,Oracle,Oracle11g,我用多列创建了一个表,每列都有单独的查询。我如何添加这个 select (select count(x) from table1 a, table2 b where ~~;) as column1, (select count(x) from table3 a, table4 b where ~~;) as column2, (select count(x) from table5 a, table6 b where ~~;) as column3, (column1 + column2 + c

我用多列创建了一个表,每列都有单独的查询。我如何添加这个

select
(select count(x) from table1 a, table2 b where ~~;) as column1,
(select count(x) from table3 a, table4 b where ~~;) as column2,
(select count(x) from table5 a, table6 b where ~~;) as column3,
(column1 + column2 + column3) as total
from dual;
若每个列都来自同一个表,则上述查询可以工作,但在本例中,Oracle SQL告诉我column1、2、3是总计行中的无效标识符

我该怎么做

您可以使用子查询:

select *, column1+column2+column3 as total from
(
select
(select count(x) from table1 a, table2 b where ...) as column1,
(select count(x) from table3 a, table4 b where ...) as column2,
(select count(x) from table5 a, table6 b where ...) as column3
from dual
) X
您可以使用子查询:

select *, column1+column2+column3 as total from
(
select
(select count(x) from table1 a, table2 b where ...) as column1,
(select count(x) from table3 a, table4 b where ...) as column2,
(select count(x) from table5 a, table6 b where ...) as column3
from dual
) X

使用公共表表达式:

with cte as (
  select (select count(x) from table1 a, table2 b where ~~;) as column1,
         (select count(x) from table3 a, table4 b where ~~;) as column2,
         (select count(x) from table5 a, table6 b where ~~;) as column3
    from dual
)
select c.*, column1 + column2 + column3 as total
  from cte c;

使用公共表表达式:

with cte as (
  select (select count(x) from table1 a, table2 b where ~~;) as column1,
         (select count(x) from table3 a, table4 b where ~~;) as column2,
         (select count(x) from table5 a, table6 b where ~~;) as column3
    from dual
)
select c.*, column1 + column2 + column3 as total
  from cte c;

只能在order by子句中引用列别名(如此处的column1):

c_别名

指定列表达式的别名。Oracle数据库将在结果集的列标题中使用此别名。AS关键字是可选的。在查询期间,别名有效地重命名了选择列表项。别名可以在order_by_子句中使用,但不能在查询中的其他子句中使用

可以使用子查询获取计算列,然后在CTE或内联视图中使用别名在该子查询之外执行进一步的工作:

select column1, column2, column3, (column1 + column2 + column3) as total
from (
  select
    (select count(x) from table1 a, table2 b where ~~) as column1,
    (select count(x) from table3 a, table4 b where ~~) as column2,
    (select count(x) from table5 a, table6 b where ~~) as column3
  from dual
);

只能在order by子句中引用列别名(如此处的column1):

c_别名

指定列表达式的别名。Oracle数据库将在结果集的列标题中使用此别名。AS关键字是可选的。在查询期间,别名有效地重命名了选择列表项。别名可以在order_by_子句中使用,但不能在查询中的其他子句中使用

可以使用子查询获取计算列,然后在CTE或内联视图中使用别名在该子查询之外执行进一步的工作:

select column1, column2, column3, (column1 + column2 + column3) as total
from (
  select
    (select count(x) from table1 a, table2 b where ~~) as column1,
    (select count(x) from table3 a, table4 b where ~~) as column2,
    (select count(x) from table5 a, table6 b where ~~) as column3
  from dual
);

你确定分号吗?我会使用边线,即从双边线中选择c1,c2,c3,c1+c2+c3,边线选择。。。作为来自…的c1,侧向选择。。。作为来自…的c2,侧向选择。。。作为c3来自。。。你确定分号吗?我会使用边线,即从双边线中选择c1,c2,c3,c1+c2+c3,边线选择。。。作为来自…的c1,侧向选择。。。作为来自…的c2,侧向选择。。。作为c3来自。。。是的,我也在考虑这个问题,但实际的查询很长。lol.每列由4个以上的表连接而成,大约有20列。因此,如果我在上面使用另一个查询,它将使这个查询长2倍,因此我想知道是否有不同的方法,而不是有1000行查询:像整个查询一样的数据声音需要重构*8——至少在子查询引用相同的表时是这样。外部查询只需要引用内部查询中的别名,这样就不会重复整个过程;这里的计算总数是从内部查询中移出的,因此不会重复。尽管如此,CTE可能比内联视图更容易管理,即使它们在做什么方面没有真正的区别。是的,我也在考虑这个问题,但实际的查询非常长。lol.每列由4个以上的表连接而成,大约有20列。因此,如果我在上面使用另一个查询,它将使这个查询长2倍,因此我想知道是否有不同的方法,而不是有1000行查询:像整个查询一样的数据声音需要重构*8——至少在子查询引用相同的表时是这样。外部查询只需要引用内部查询中的别名,这样就不会重复整个过程;这里的计算总数是从内部查询中移出的,因此不会重复。尽管如此,CTE可能比内联视图更容易管理,即使它们在做什么方面没有真正的区别。