如何在一个视图中显示多个查询结果?

时间:2013-12-05 09:41:54

标签: sql oracle-sqldeveloper

我正在尝试在一个视图中显示多个选择查询的结果。我试图谷歌它没有成功。有什么建议吗?

示例:

查询1

SELECT REGEXP_COUNT( column1, '</Expression>' ) AS Col1
  FROM table1 
 WHERE column1 like '%Group1%'

查询提供如下内容:

Col1
1000
1000
1000
135

查询2

SELECT REGEXP_COUNT( column1, '</Expression>' ) AS Col2
  FROM table1
 WHERE column1 like '%Group2%'

结果:

Col2
1000
1000
1000
1000
1000
1000
46

我想在一个视图中使用单独的列显示两个查询的计数结果。结果也以多行而不是一行显示。如何总结所有行的数量?所有建议赞赏!最终结果应该是这样的

Col1 | COL2
3135 | 5046

1 个答案:

答案 0 :(得分:1)

也许UNION或UNION ALL会有所帮助?

SELECT REGEXP_COUNT( column1, '</Expression>' )
  FROM table1
 WHERE column1 like '%Group1%'
UNION ALL
SELECT REGEXP_COUNT( column1, '</Expression>' )
  FROM table1
 WHERE column1 like '%Group2%';

如果你真的需要专栏,可能就像:

SELECT
  A.cnt1,
  b.cnt2
FROM
  (
    SELECT
      REGEXP_COUNT( column1, '</Expression>' ) cnt1,
      ROWNUM AS rwn
    FROM
      table1
    WHERE
      column1 LIKE '%Group1%'
  )
  a
FULL OUTER JOIN
  (
    SELECT
      REGEXP_COUNT( column1, '</Expression>' ) cnt2,
      ROWNUM AS rwn
    FROM
      table1
    WHERE
      column1 LIKE '%Group2%'
  )
  b
ON
  a.rwn = b.rwn;

对于您添加的SUMS,您可以再次在SELECT中使用简单的子选择。

SELECT
  (
    SELECT
      SUM(REGEXP_COUNT( column1, '</Expression>' )) AS Col1
    FROM
      table1
    WHERE
      column1 LIKE '%Group1%'
  ) AS res1,
  (
    SELECT
      SUM(REGEXP_COUNT( column1, '</Expression>' )) AS Col1
    FROM
      table1
    WHERE
      column1 LIKE '%Group2%'
  ) AS res2
FROM
dual;