我们有来自客户端的与sql相关的请求

时间:2016-08-09 11:22:07

标签: sql oracle

我们必须找到列C1和C2中出现的所有字段。

表客户端

C1     C2
A      X
A      X
B      Y
B      Y

输出表

C1  C2
A    2
B    2
X    2
Y    2

我们有包含团队数据的单列表。我们希望找到各种各样的比赛组合。

C1
A
B
C
D

输出表

C1
AB
AC
AD
BC
BD
CD

1 个答案:

答案 0 :(得分:1)

如果我正确理解了2个问题......

SQL> CREATE TABLE tab1 AS
  2  WITH t AS
  3   (SELECT 'A' c1, 'X' c2
  4    FROM   dual
  5    UNION ALL
  6    SELECT 'A', 'X'
  7    FROM   dual
  8    UNION ALL
  9    SELECT 'B', 'Y'
 10    FROM   dual
 11    UNION ALL
 12    SELECT 'B', 'Y'
 13    FROM   dual)
 14  SELECT * FROM t;

Table created.

SQL>
SQL> SELECT * FROM tab1 ORDER BY c1, c2;

C1  C2
--- ---
A   X
A   X
B   Y
B   Y

4 rows selected.

SQL>
SQL> SELECT c1, COUNT(*) c2
  2  FROM   (SELECT c1 FROM tab1 UNION ALL SELECT c2 FROM tab1)
  3  GROUP  BY c1
  4  ORDER  BY c1;

C1   C2
--- ---
A     2
B     2
X     2
Y     2

4 rows selected.

第二部分

SQL> CREATE TABLE tab2 AS
  2  WITH t AS
  3   (SELECT CAST(column_value AS VARCHAR2(3)) c1
  4    FROM   TABLE(sys.odcivarchar2list('A', 'B', 'C', 'D')))
  5  SELECT * FROM t;

Table created.

SQL>
SQL>
SQL> SELECT * FROM tab2 ORDER BY c1;

C1
---
A
B
C
D

4 rows selected.

SQL> SELECT t1.c1 || t2.c1 c1 FROM tab2 t1 JOIN tab2 t2 ON t1.c1 < t2.c1;

C1
---
AB
AC
AD
BC
BD
CD

6 rows selected.