【问题标题】:sql or python solution for getting merging duplicate rows into one in an ordered table and reordering themsql 或 python 解决方案,用于将重复行合并为有序表中的一个行并对其重新排序
【发布时间】:2022-11-23 04:12:19
【问题描述】:

有人可以帮我在 SQL 中的 select 语句中执行此操作吗? 我有一个表 xyz 如下:

ColumnID Column A Column B
1 1 A
1 2 B
1 3 C
1 4 D
2 1 A
2 2 B
2 3 C
2 4 C
3 1 A
3 2 A
3 3 B
3 4 B
4 1 A
4 2 B
4 3 V
4 4 V

我想让它变成这样:

Column A Column B
1 A
2 B
3 C
4 D
1 A
2 B
3 C
1 A
2 B
1 A
2 B
3 V

什么都没试过

【问题讨论】:

    标签: duplicates sql-order-by ordereddictionary


    【解决方案1】:

    好吧,因为我得到了反对票,所以我能够为它编写 SQL ;)我猜是因为没有先尝试。

    这是任何有相同问题的人的解决方案:

    with abc as (select  columnID, ROW_NUMBER() over (PARTITION by columnID, column_b   ORDER BY a.columnID) as column_a, column_b 
                    from xyz)
        select  row_number() over (partition by columnID order by column_a asc) as column_a, column_b
        from abc where row_num = 1
    

    【讨论】:

      猜你喜欢
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      相关资源
      最近更新 更多