簡體   English   中英

Oracle 11g:如何將oracle表與自身結合起來?

[英]Oracle 11g: How to union an oracle table with itself?

我有一個表如下所示:

date          code                name    score         set
09/09/12     967873         Team A         24            1
09/09/12     967873         Team B         22            1
09/09/12     967873         Team A         21            2 
09/09/12     967873         Team B         16            2
02/04/12     965454         Team X         21            1
02/04/12     965454         Team Y         19            1
02/04/12     965454         Team X         21            2
02/04/12     965454         Team Y         19            2

你猜對了! 這是一場排球比賽! 但是,我希望我的輸出只能在一行中。 例如:

date           code               Teams                 Set-1    Set-2     Set-3
09/09/12      967873             Team A VS.Team B       24-22    21-16       -
and so on.... 

**Notice that the game could have a third set as well

我需要某種自聯接來將上述格式細化為用戶視圖更容易的格式...如果您需要更多詳細信息,請告訴我。

謝謝,

查詢可能如下所示:

with matches as (
   select "DATE", code, name,
      max(case when "SET" = 1 then score end) score_1,
      max(case when "SET" = 2 then score end) score_2,
      max(case when "SET" = 3 then score end) score_3,
      row_number() over(partition by "DATE", code order by name) team_no
    from games
    group by "DATE", code, name
)
select a."DATE", a.code, a.name || ' vs. ' || b.name teams,
  a.score_1 || '-' || b.score_1 set_1,
  a.score_2 || '-' || b.score_2 set_2,
  a.score_3 || '-' || b.score_3 set_3
from matches a
join matches b on a."DATE" = b."DATE" and a.code = b.code
where a.team_no = 1 and b.team_no = 2;

日期集合是相當不幸的列名。

該查詢分為3個步驟:

  1. 匯總記錄以為每個團隊創建一行並匹配。 在該過程中,分數被分配給三個列set_1,set_2,set_3中的一個。
  2. 行號分配給每一行,每個匹配從1開始。 結果是一個團隊被分配1,另一個團隊被分配2(列team_no)。
  3. 結果表連接到自身,左側是沒有的團隊。 1和沒有的球隊的右側。 2使用匹配(日期和代碼)作為連接條件。 結果是每場比賽一行,兩隊的名稱和分數。

首先,按"date", code, "set"將數據分組到LISTAGG團隊和分數。 然后在結果列上旋轉結果。 這是它的SQL:

WITH grouped AS (
  SELECT
    "date", code, "set",
    LISTAGG(name,  ' VS. ') WITHIN GROUP (ORDER BY name) AS teams,
    LISTAGG(score, '-'    ) WITHIN GROUP (ORDER BY name) AS score
  FROM matches
  GROUP BY
    "date", code, "set"
)
,    pivoted AS (
  SELECT
    "date", code, teams,
    nvl("1", '-') AS set1,
    nvl("2", '-') AS set2,
    nvl("3", '-') AS set3
  FROM grouped
  PIVOT (
    MAX(score) FOR "set" IN (1, 2, 3)
  ) p
)
SELECT * FROM pivoted
;

在SQL Fiddle上查看此查詢。

我會這樣做,如果表的名字讓我們說VOLLEYBALL:

SELECT temp.date, temp.code, 
    temp.team1 || ' vs. ' || temp.team2 AS teams, 
    (SELECT v.score FROM volleyball v WHERE v.code = temp.code AND v.name = team1 AND v.set = 1) || '-' || 
        (SELECT v.score FROM volleyball v WHERE v.code = temp.code AND v.name = team2 AND v.set = 1) AS set1, 
    (SELECT v.score FROM volleyball v WHERE v.code = temp.code AND v.name = team1 AND v.set = 2) || '-' || 
        (SELECT v.score FROM volleyball v WHERE v.code = temp.code AND v.name = team2 AND v.set = 2) AS set2, 
    nvl((SELECT v.score FROM volleyball v WHERE v.code = temp.code AND v.name = team1 AND v.set = 3) || '-' || 
        (SELECT v.score FROM volleyball v WHERE v.code = temp.code AND v.name = team2 AND v.set = 3)
        , '-') AS set3 -- optional, if no results, then it will be a '-' 
FROM
    (SELECT v.date, v.code, 
        min(v.name) AS team1,  max(v.name) AS team2 
    FROM volleyball v 
    GROUP BY v.date, v.code) temp; 

這將產生一行摘要。

要在不需要連接的情況下返回所需結果,請嘗試:

select "date", 
       code, 
       min_name || ' VS. ' || max_name teams,
       sum(case when "set" = 1 and name = min_name then score end) || '-' || 
          sum(case when "set" = 1 and name = max_name then score end) "Set-1",
       sum(case when "set" = 2 and name = min_name then score end) || '-' || 
          sum(case when "set" = 2 and name = max_name then score end) "Set-2",
       sum(case when "set" = 3 and name = min_name then score end) || '-' || 
          sum(case when "set" = 3 and name = max_name then score end) "Set-3"
from (select g.*,
             min(name) over (partition by "date", code) min_name,
             max(name) over (partition by "date", code) max_name
      from games)
group by "date", code

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM