如何使用“case”语句/ cross tab函数正确透视PostgreSQL中的表数据

bnl4lu3b  于 2023-02-08  发布在  PostgreSQL
关注(0)|答案(2)|浏览(187)

我有一个表,其中包含在特定年份(2018 - 2022)添加到播放列表的艺术家列表。我想将数据透视到五个"年份"列,并在这些年份添加相应的艺术家姓名。我使用了"case"语句。但是,它每行显示一条记录,并为所有其他年份提供空值。
预期产出:

所以我尝试了"crosstab"函数,但是它仍然不能像预期的那样输出数据,这些数据位于不同的列中,对应于没有任何空值的空白字段的年份。

7nbnzgx9

7nbnzgx91#

通过将屏幕截图拼接在一起,您似乎得到了一个包含三列的表:(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                          |

如果是这样,下面的查询可能对您有用:

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;
sycxhyv7

sycxhyv72#

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

给出输出:

相关问题