我在postgresql-9.1.x数据库中有一个表,其定义如下:
# \d cms
Table "public.cms"
Column | Type | Modifiers
-------------+-----------------------------+--------------------------------------------------
id | integer | not null default nextval('cms_id_seq'::regclass)
last_update | timestamp without time zone | not null default now()
system | text | not null
owner | text | not null
action | text | not null
notes | text
以下是表格中的数据样本:
id | last_update | system | owner | action |
notes
----+----------------------------+----------------------+-----------+------------------------------------- +-
----------------
584 | 2012-05-04 14:20:53.487282 | linux32-test5 | rfell | replaced MoBo/CPU |
34 | 2011-03-21 17:37:44.301984 | linux-gputest13 | apeyrovan | System deployed with production GPU |
636 | 2012-05-23 12:51:39.313209 | mac64-cvs11 | kbhatt | replaced HD |
211 | 2011-09-12 16:58:16.166901 | linux64-test12 | rfell | HD swap |
drive too small
我想做的是制作一个SQL查询,它只返回系统和所有者列中的唯一/不同值(如果一列结果中的值数小于另一列的结果,则填入NULL ),而忽略了它们之间的关联。所以像这样:
system | owner
-----------------+------------------
linux32-test5 | apeyrovan
linux-gputest13 | kbhatt
linux64-test12 | rfell
mac64-cvs11 |
我可以找到获取此数据的唯一方法是使用两个单独的SQL查询: SELECT系统FROM cms GROUP BY系统; SELECT owner FROM cms GROUP BY所有者;
答案 0 :(得分:2)
很难从我这里询问你为什么要做这样的事情。以下查询通过使用row_number()函数在计算列上执行连接来执行此操作:
select ts.system, town.owner
from (select system, row_number() over (order by system) as seqnum
from (select distinct system
from t
) ts
) ts full outer join
(select owner, row_number() over (order by owner) as seqnum
from (select distinct owner
from t
) town
) town
on ts.seqnum = town.seqnum
完整的外部联接可确保完整返回两个列表中较长的一个。