微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

取消雪花中的多个列

如何解决取消雪花中的多个列

我有一个表,如下所示:

enter image description here

我需要如下取消“评级”和“评论”:

enter image description here

在雪花中执行此操作的最佳方法是什么?

注意:评论列中有一些单元格为NULL

添加详细信息:

create or replace table reviews(name varchar(50),acting_rating int,acting_comments text,comedy_rating int,comedy_comments text);

insert into reviews values
    ('abc',4,NULL,1,'NO'),('xyz',3,'some','haha'),('lmn','what',NULL);
    
    select * from reviews;
    


select name,skill,skill_rating,comments
    from reviews
    unpivot(skill_rating for skill in (acting_rating,comedy_rating)) 
    unpivot(comments for skill_comments in (acting_comments,comedy_comments)) 

--Following where clause is added to filter the irrelevant comments due to multiple unpivots

where substr(skill,position('_',skill)-1) = substr(skill_comments,skill_comments)-1) 
     order by name;

会产生预期的结果,但是对于具有NULL的数据,输出中将丢失具有NULL的未透视行:

NAME    SKILL   SKILL_rating    COMMENTS
abc COMEDY_rating   1   NO
lmn ACTING_rating   1   what
xyz ACTING_rating   3   some
xyz COMEDY_rating   1   haha

解决方法

这是一个基本脚本,应提供所需的输出

create or replace table reviews(name varchar(50),acting_rating int,acting_comments text,comedy_rating int,comedy_comments text);

insert into reviews values
    ('abc',4,'something',1,'NO'),('xyz',3,'some','haha'),('lmn','what','hahaha');
    
    select * from reviews;
    


select name,skill,skill_rating,comments
    from reviews
    unpivot(skill_rating for skill in (acting_rating,comedy_rating)) 
    unpivot(comments for skill_comments in (acting_comments,comedy_comments)) 

--Following where clause is added to filter the irrelevant comments due to multiple unpivots

where substr(skill,position('_',skill)-1) = substr(skill_comments,skill_comments)-1) 
     order by name;

,

如果您要解决的只是问题中指定的表-您可以使用一组UNION ALL手动进行处理:

select NAME,'ACTING_RATING' as SKILL,ACTING_RATING as SKILL_RATING,ACTING_COMMENTS as SKILL_COMMENTS
from DATA
union all
select NAME,'COMEDY_RATING',COMEDY_RATING,COMEDY_COMMENTS
from DATA
union all
select NAME,'MUSICAL_PERFORMANCE_RATING',MUSICAL_PERFORMANCE_RATING,MUSICAL_PERFORMANCE_COMMENTS
from DATA

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。