【问题标题】:Can I get this result in postgresql?我可以在 postgresql 中得到这个结果吗?
【发布时间】:2017-03-08 04:31:36
【问题描述】:

我有一张桌子

是否可以有table2:

【问题讨论】:

  • 我在 column1 中有 2 次相同的属性,我只想拥有一次。我不知道这是否有助于找到解决方案。对不起。。
  • OUPS.. 第一个表 table1 只有 2 列(column1 和 column1 2),我想创建第二个表.. 我没有规则.. 我得到了 table1,我想要创建表2。无论如何,即使没有解决方案也谢谢
  • 先试后问。

标签: postgresql


【解决方案1】:

想到的一个解决方案涉及子查询和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

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 2022-11-25
    • 2020-08-28
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    相关资源
    最近更新 更多