我可以从其他表中选择一行而不是当前推荐的吗?

时间:2013-12-10 09:50:57

标签: sql oracle

假设我有2个表“A”和“B”,我的查询如下。

Select 
count( a."col_A1") as ACount, 
Sum(a."col_A2") as ASum, 
(Select count(b."col_B1") from B b) as BCount 
From A a

我无法在Oracle中执行上述语句,但它在MSSQL中运行良好。

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

这是一个有效的构造,在聚合结果中包含一个子查询,但它看起来很特殊。

我将其构建为:

select
  a.c_col1,
  a.s_col2,
  b.c_col3
from
  (select count(col1) c_col1,
          sum(col2)   s_col2
   from   table_a) a,
  (select count(col3) c_col3
   from   table_b) b

答案 1 :(得分:1)

只需在查询末尾添加GROUP BY 1

Select count( a."col_A1") as ACount
,      sum(a."col_A2") as ASum
,      (Select count(b."col_B1") from B b) as BCount 
From A a
GROUP BY 1