在表格中创建单向对齐条目(例如,运动队)

时间:2018-04-28 19:35:06

标签: sql oracle11g

我有表SOCER,并且有一个列TEAM,如下所示。

Team
--------
Germany
Brazil
Argentina
Italy

他们需要与对手球队进行比赛,如以下所需的输出:

---------  ----------------
Germany    Brazil
Germany    Argentina
Germany    Italy
Brazil     Argentina
Brazil     Italy
Argentina  Italy

我该如何撰写查询?

5 个答案:

答案 0 :(得分:2)

使用分层查询不需要自联接,只需要一次表扫描:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE Soccer ( Team ) AS
SELECT 'Germany' FROM DUAL UNION ALL
SELECT 'Brazil' FROM DUAL UNION ALL
SELECT 'Argentina' FROM DUAL UNION ALL
SELECT 'Italy' FROM DUAL;

查询1

SELECT prior team AS team,
       team AS opponent
FROM   (
  SELECT team, ROWNUM AS rn
  FROM   Soccer
)
WHERE LEVEL = 2
CONNECT BY prior rn < rn

<强> Results

|      TEAM |  OPPONENT |
|-----------|-----------|
|   Germany |    Brazil |
|   Germany | Argentina |
|   Germany |     Italy |
|    Brazil | Argentina |
|    Brazil |     Italy |
| Argentina |     Italy |

查询2 :或者更简单,但使用不同的排序

SELECT prior team AS team,
       team AS opponent
FROM   Soccer
WHERE LEVEL = 2
CONNECT BY prior team < team

<强> Results

|      TEAM | OPPONENT |
|-----------|----------|
| Argentina |   Brazil |
| Argentina |  Germany |
| Argentina |    Italy |
|    Brazil |  Germany |
|    Brazil |    Italy |
|   Germany |    Italy |

答案 1 :(得分:1)

SELECT soccer.team, soccer2.team
FROM soccer, soccer as soccer2
WHERE soccer.team > soccer2.team

快速而肮脏。如果要按特定顺序订购,请添加ORDER BY,例如:

SELECT soccer.team, soccer2.team
FROM soccer, soccer as soccer2
WHERE soccer.team > soccer2.team
ORDER BY soccer.team, soccer2.team

不是你的问题,但另一个有用的查询是对你有回归比赛的比赛的查询(即球队互相比赛两次:

SELECT soccer.team, soccer2.team
FROM soccer, soccer as soccer2
WHERE soccer.team <> soccer2.team

它基本上是相同的查询,但&lt;&gt;而不是&gt;

答案 2 :(得分:1)

SQL> with soccer (team) as
  2    (select 'Germany'    from dual union
  3     select 'Brazil'     from dual union
  4     select 'Argentina'  from dual union
  5     select 'Italy'      from dual
  6    )
  7  select h.team, g.team
  8  from soccer h cross join soccer g
  9  where h.team > g.team;

TEAM      TEAM
--------- ---------
Italy     Germany
Italy     Brazil
Italy     Argentina
Germany   Brazil
Germany   Argentina
Brazil    Argentina

6 rows selected.

SQL>

答案 3 :(得分:1)

如果您真的需要所需的输出以匹配您的问题,那么您需要在ROWNUM上匹配,而不是在团队名称上匹配:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE SOCER
    ("Team" varchar2(9))
;

INSERT ALL 
    INTO SOCER ("Team")
         VALUES ('Germany')
    INTO SOCER ("Team")
         VALUES ('Brazil')
    INTO SOCER ("Team")
         VALUES ('Argentina')
    INTO SOCER ("Team")
         VALUES ('Italy')
SELECT * FROM dual
;

查询1

SELECT a."Team" AS Team1, b."Team" AS Team2
FROM 
  (SELECT "Team", ROWNUM AS rn FROM SOCER) a
  INNER JOIN
  (SELECT "Team", ROWNUM AS rn FROM SOCER) b
    ON b.rn > a.rn;

<强> Results

|     TEAM1 |     TEAM2 |
|-----------|-----------|
|   Germany |    Brazil |
|   Germany | Argentina |
|   Germany |     Italy |
|    Brazil | Argentina |
|    Brazil |     Italy |
| Argentina |     Italy |

答案 4 :(得分:-1)

您尝试此代码

select a.team, b.team from socer a, socer b;