简体   繁体   English

如何使用 case 语句/交叉表函数正确地透视 postgres 中的表数据

[英]How to correctly pivot the table data in postgres using case statements/ cross tab function

I have a table comprising a list of artists added to a playlist during specific years (2018 - 2022).我有一张表格,其中包含在特定年份(2018 - 2022 年)添加到播放列表的艺术家列表。 I want to pivot the data into 5 "year" columns with corresponding artist names added in those years.我想将数据转换为 5 个“年”列,并在这些年中添加相应的艺术家姓名。 I used the case statements however it shows one record per row and give a null value for all other years.我使用了 case 语句,但它每行显示一条记录,并为所有其他年份提供空值。

Expected output:预期输出: 在此处输入图像描述

使用 case 语句给我空值

So I tried crosstab function but still doesnt output data as expected which is in different columns corresponding to years without any blank fields with null values.所以我尝试了交叉表功能,但仍然没有按预期输出数据,这些数据位于与年份对应的不同列中,没有任何具有空值的空白字段。 在此处输入图像描述

By piecing the screenshots together, it seems that you have a table with three columns: ( year_added , track , artist ) and you want the output looks like below:通过将屏幕截图拼凑在一起,您似乎有一个包含三列的表格:( year_addedtrackartist )并且您希望输出如下所示:

track                             |2018                               |2019                                          |2020                             |2021                               |
----------------------------------+-----------------------------------+----------------------------------------------+---------------------------------+-----------------------------------+
Alors on dance                    |Doug,Kieth,Roger                   |Alan,Chester,William                          |Nicholas                         |Maxwell                            |
Andalouse                         |Tyson                              |Daron,Domenic,Ramon                           |Barney,Chuck,Nicholas            |                                   |
Baila Morena                      |                                   |Hayden,Sebastian                              |                                 |                                   |
Bailando                          |Brad                               |Chester,Elijah,George,Julian                  |Barry,Roger                      |Doug,Jack,Kurt,Matt,Rick,Rocco     |
Beat it                           |Chris,Daniel                       |Denis                                         |Tom                              |Bryon,John,Mike                    |
Beggin                            |Anthony                            |Chad,Mike,Noah                                |Josh,Rufus                       |Denis                              |
Bette Davis Eyes                  |Abdul,Eduardo                      |Carter,Jacob                                  |Gil                              |Erick,Ron                          |

If so, the query below may work for you:如果是这样,下面的查询可能对您有用:

with cte as (
select track,
       case when year_added = 2018 then string_agg(artist,',') end as "2018",
       case when year_added = 2019 then string_agg(artist,',') end as "2019",
       case when year_added = 2020 then string_agg(artist,',') end as "2020",
       case when year_added = 2021 then string_agg(artist,',') end as "2021",
       case when year_added = 2022 then string_agg(artist,',') end as "2022"
  from tab_year_added
 group by track, year_added)
select track,
       max("2018") as "2018",
       max("2019") as "2019",
       max("2020") as "2020",
       max("2021") as "2021",
       max("2022") as "2022"
  from cte
 group by track
 order by track;

However, if it's not the case, I would suggest you share more details (in text format) on但是,如果不是这种情况,我建议您分享更多详细信息(以文本格式)

  • Table definition表定义
  • Sample data样本数据
  • Expected outcome预期结果
select max("2018") as "2018",max("2019") as "2019",max("2020") as "2020",max("2021")as "2021", max("2022") as "2022" from(
select
    YEAR_ADDED,
    row_number() over(partition by year_added order by artists_list) as rn,
    case when YEAR_ADDED = '2018' then artists_list   else null end as "2018",
    case when YEAR_ADDED = '2019' then artists_list else null end as "2019",
    case when YEAR_ADDED = '2020' then artists_list else null end as "2020",
    case when YEAR_ADDED = '2021' then artists_list   else null end as "2021",
    case when YEAR_ADDED = '2022' then artists_list else null end as "2022"
from year_added group by year_added,  artists_list) as temp
group by rn order by rn

Gives output:给出输出:

在此处输入图像描述

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

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