我使用 1 個表進行了 4 次交叉連接,給了我 5 列資料。我在所有 4 個交叉連接中使用相同的列來獲得每個組合。唯一的問題是我以不同的順序獲得相同的資料。我試圖只獲取唯一的記錄。
我只能對每組資料進行一次迭代,即 12345 - 12346 - 12347,但不能有 54321 - 64321 - 74321 或任何組合所有相同數字的組合。我難住了。
| 表 1/第 1 列 |
|---|
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| ... |
| 100 |
Select t.col1, t1.col1, t2.col1, t3.col1, t4.col1
from table1 t
CROSS JOIN table1 t1
CROSS JOIN table1 t2
CROSS JOIN table1 t3
CROSS JOIN table1 t4
WHERE
t.col1 != t1.col1 and t.col1 != t2.col1 and t.col1 != t3.col1 and t.col1 != t4.col1
and t1.col1 != t2.col1 and t1.col1 != t3.col1 and t1.col1 != t4.col1
and t2.col1 != t3.col1 and t2.col1 != t4.col1
and t3.col1 != t4.col1
| 第 1 列 | 列2 | 第 3 列 | 第 4 列 | 第 5 列 |
|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 |
| 2 | 1 | 3 | 4 | 5 |
| 3 | 2 | 1 | 4 | 5 |
| 5 | 2 | 4 | 3 | 1 |
| 5 | 4 | 3 | 2 | 1 |
只有這些作品中的一個,第一個,因為其余的都只是第一個的組合。想想有數百行的原始表。
uj5u.com熱心網友回復:
全部替換=!為<:
Select t.col1, t1.col1, t2.col1, t3.col1, t4.col1
from table1 t
CROSS JOIN table1 t1
CROSS JOIN table1 t2
CROSS JOIN table1 t3
CROSS JOIN table1 t4
WHERE
t.col1 < t1.col1 and t.col1 < t2.col1 and t.col1 < t3.col1 and t.col1 < t4.col1
and t1.col1 < t2.col1 and t1.col1 < t3.col1 and t1.col1 < t4.col1
and t2.col1 < t3.col1 and t2.col1 < t4.col1
and t3.col1 < t4.col1
問題=!是當你防止數字 x 和 y 相同時,你會得到兩個連接;一種用于 x < y 的情況,一種用于 x > y 的情況,導致不需要的重復連接。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/364283.html
標籤:sql sql-server 查询语句
上一篇:使用嵌套查詢簡化查詢
下一篇:XQuery檢索值
