简体   繁体   English

SQL 查询根据 ID 将多行合并为一行,同时将其他值保留在同一行中?

[英]SQL Query to combine multiple rows into one based on ID while keeping other value in the same row?

first of all, I have been searching for this for some time.首先,我一直在寻找这个。

I have a table that looks kinda like this:我有一个看起来有点像这样的表:

ID        Expenditure  MonthYear
1A        1,000        122019
1A        1,500        012020
1B        1,900        122019
1C        2,400        122019
1B        2,400        012020
1C        900          012020
1A        800          022020

Since the rows can reach to thousands, and some IDs are repeated tens of times, I want to combine those with distinct ID into a single row and add columns which retain all the information in it.由于行可以达到数千,并且某些 ID 重复了数十次,我想将具有不同 ID 的那些组合成一行并添加保留其中所有信息的列。 I want to make the table to looks something like this:我想让表格看起来像这样:

ID        Expenditure_1  MonthYear_1 Expenditure_2 MonthYear_2  Expenditure_3   MonthYear_3
1A        1,000          122019      1,500         012020       800             022020
1B        1,900          122019      2,400         012020       Null            Null
1C        2,400          122019      900           012020       Null            Null

What is the best way in approaching this problem using SQL on Impala?在 Impala 上使用 SQL 解决此问题的最佳方法是什么? Thank you.谢谢你。

You can use conditional aggregation and row_number():您可以使用条件聚合和 row_number():

select id,
       max(case when seqnum = 1 then expenditure end) as expenditure_1,
       max(case when seqnum = 1 then monthyear end) as monthyear_1,
       max(case when seqnum = 2 then expenditure end) as expenditure_2,
       max(case when seqnum = 2 then monthyear end) as monthyear_2,
       max(case when seqnum = 3 then expenditure end) as expenditure_3,
       max(case when seqnum = 3 then monthyear end) as monthyear_3
from (select t.*,
             row_number() over (partition by id order by right(monthyear, 4), left(monthyear, 2)) as seqnum
      from t
     ) t
group by id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM