嘗試以下操作:
SELECT DISTINCT COL1 AS COL1, NULL AS COL2, NULL AS COL3, NULL AS COL4,
NULL AS COL5, NULL AS COL6 FROM TABLE1
UNION ALL
SELECT NULL AS COL1, DCOL2 AS COL2, NULL AS COL3, NULL AS COL4,
NULL AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL2 AS DCOL2
FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, DCOL3 AS COL3, NULL AS COL4,
NULL AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL3 AS DCOL3
FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, NULL AS COL3, DCOL4 AS COL4,
NULL AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL4 AS DCOL4
FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, NULL AS COL3, NULL AS COL4,
DCOL5 AS COL5, NULL AS COL6 FROM (SELECT DISTINCT COL5 AS DCOL5
FROM TABLE1)
UNION ALL
SELECT NULL AS COL1, NULL AS COL2, NULL AS COL3, NULL AS COL4,
NULL AS COL5, DCOL6 AS COL6 FROM (SELECT DISTINCT COL6 AS DCOL6
FROM TABLE1);
分享和享受。
你可以給出一個表中的內容嗎?如果你得到與SELECT *相同的結果,那麼COL1中的所有記錄都是不同的。 – ProfessionalAmateur 2012-01-04 22:08:16
你想檢查[這個問題](http://stackoverflow.com/questions/54418/how-do-i-or-can-i-select-distinct-on-multiple-columns-postgresql)?看起來非常相似 – 2012-01-04 22:09:04
請記住,DISTINCT應用於您要返回的整個列集,而不僅僅是第一列。因此,例如'(1,2,3,4,5,6)'與[(1,2,3,4,5,7)'不同),因爲第六列在兩行之間是不同的。 – 2012-01-04 22:10:21