簡體   English   中英

sql多次選擇和加入行

[英]sql selecting and joining rows multiple times

我的查詢多次選擇行時遇到問題,而我的左聯接多次聯接行。

我正在查詢的表看起來像這樣。 該表按wins分區(這是示例數據)

+-----------+------------+------+---------+------+--+
| Player_ID |    name    | wins | matches | nrow |  |
+-----------+------------+------+---------+------+--+
|      4070 | Twilight   | 1L   | 1L      | 5L   |  |
|      4073 | Pinkie Pie | 1L   | 1L      | 3L   |  |
|      4071 | Fluttershy | 1L   | 1L      | 4L   |  |
|      4077 | jim        | 1L   | 1L      | 2L   |  |
|      4075 | mike       | 1L   | 1L      | 1L   |  |
|      4076 | sam        | 0L   | 1L      | 2L   |  |
|      4072 | Applejack  | 0L   | 1L      | 1L   |  |
|      4074 | gav        | 0L   | 1L      | 3L   |  |
+-----------+------------+------+---------+------+--+

查詢看起來像這樣

SELECT player_standings.player_ID, matched_player.player_ID, player_standings.wins, player_standings.nrow, matched_player.nrow
FROM player_standings LEFT JOIN (
    SELECT player_ID, wins, nrow FROM player_standings
) AS matched_player ON
(matched_player.wins = player_standings.wins and matched_player.nrow % 2 = 0)
WHERE player_standings.nrow % 2 = 1;

每隔第二行選擇匹配的玩家,以避免在所有行的player1和player2列之間獲得重復的值。 通過使用不等式運算符,我無法實現同一目標。

我得到的結果看起來像這樣

+---------+---------+------+--------------+--------------+
| player1 | player2 | wins | player1 nrow | player2 nrow |
+---------+---------+------+--------------+--------------+
|    4070 |    4071 | 1L   | 5L           | 4L           |
|    4075 |    4071 | 1L   | 1L           | 4L           |
|    4073 |    4077 | 1L   | 3L           | 2L           |
|    4074 |    4076 | 0L   | 3L           | 2L           |
|    4075 |    4077 | 1L   | 1L           | 2L           |
|    4073 |    4071 | 1L   | 3L           | 4L           |
|    4072 |    4076 | 0L   | 1L           | 2L           |
|    4070 |    4077 | 1L   | 5L           | 2L           |
+---------+---------+------+--------------+--------------+

注意播放器1和播放器2的重復值。

總而言之,我想要的是讓player1在每一行都有唯一的值。 對於player2,我希望每一行都有一個唯一值,或者如果查詢中沒有匹配的行,則要具有值“ null”。

我想要的結果應該看起來像這樣

+---------+---------+------+--------------+--------------+
| player1 | player2 | wins | player1 nrow | player2 nrow |
+---------+---------+------+--------------+--------------+
|    4070 |    4071 | 1L   | 5L           | 4L           |
|    4073 |    4077 | 1L   | 3L           | 2L           |
|    4074 |    4076 | 0L   | 3L           | 2L           |
|    4075 |    null | 1L   | 1L           | null         |
|    4072 |    null | 0L   | 1L           | null         |
+---------+---------+------+--------------+--------------+

我想你是這個。 基本上,據我所理解,您希望有一個報告以兩列模式顯示信息。 通常,這類查詢是在窗口函數的幫助下解決的。 我用了ROW_NUMBER()

SQL小提琴

PostgreSQL 9.3模式設置

CREATE TABLE player_standings
    (player_ID int, name varchar(10), wins varchar(2), matches varchar(2), nrow varchar(2))
;

INSERT INTO player_standings
    (player_ID, name, wins, matches, nrow)
VALUES
    (4070, 'Twilight', '1L', '1L', '5L'),
    (4073, 'Pinkie Pie', '1L', '1L', '3L'),
    (4071, 'Fluttershy', '1L', '1L', '4L'),
    (4077, 'jim', '1L', '1L', '2L'),
    (4075, 'mike', '1L', '1L', '1L'),
    (4076, 'sam', '0L', '1L', '2L'),
    (4072, 'Applejack', '0L', '1L', '1L'),
    (4074, 'gav', '0L', '1L', '3L')
;

查詢1

SELECT player_standings1.player_ID, matched_player.player_ID, 
player_standings1.wins, player_standings1.nrow, matched_player.nrow
FROM (select *, ROW_NUMBER() OVER(PARTITION BY wins ORDER BY player_ID) rn from player_standings ) player_standings1 LEFT JOIN 
(select *, ROW_NUMBER() OVER(PARTITION BY wins ORDER BY player_ID) rn from player_standings ) matched_player 
ON player_standings1.wins = matched_player.wins AND player_standings1.rn = matched_player.rn+1
where player_standings1.rn%2=1
ORDER BY 3,1,2

結果

| player_id | player_id | wins | nrow |   nrow |
|-----------|-----------|------|------|--------|
|      4072 |    (null) |   0L |   1L | (null) |
|      4076 |      4074 |   0L |   2L |     3L |
|      4070 |    (null) |   1L |   5L | (null) |
|      4073 |      4071 |   1L |   3L |     4L |
|      4077 |      4075 |   1L |   2L |     1L |

可以使用CTE編寫相同的查詢:

with withrn as (
select *, ROW_NUMBER() OVER(PARTITION BY wins ORDER BY player_ID) rn from player_standings)
SELECT player_standings1.player_ID, matched_player.player_ID, player_standings1.wins, player_standings1.nrow, matched_player.nrow
FROM withrn player_standings1 LEFT JOIN withrn matched_player 
ON player_standings1.wins = matched_player.wins AND player_standings1.rn = matched_player.rn+1
where player_standings1.rn%2=1
ORDER BY 3,1,2

暫無
暫無

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

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