2016-10-25 43 views
-3

我有我可以在postgresql中得到这个结果吗?

enter image description here

一个table1的是,有可能有表2:

想到的

enter image description here

+0

我在列1的相同属性的2倍,我想拥有它只有一次。我不知道是否可以帮助找到解决方案..对不起.. – sam

+0

OUPS ..第一个表table1只有2列(column1和column1 2),我想创建第二个表..我没有规则..我得到了table1和我想创建table2。无论如何感谢,即使没有解决方案 – sam

+0

先试,先问。 – levi

回答

0

一种解决方案涉及的子查询和row_number解析函数:

select 
    column1, 
    max(case when rn = 1 then column2 end) as column2, 
    max(case when rn = 2 then column2 end) as column3 
from (
    select *, row_number() over (partition by column1 order by column2) as rn 
    from table1 
) t 
group by column1 

或者您可以简单地使用minmax规则像下面,但我喜欢的第一个更好:

select 
    column1, 
    min(column2) as column2, 
    max(column2) as column3 
from table1 
group by column1 
+0

谢谢..它的工作 – sam

相关问题