将多个 Case 语句组合成单个输出列
我需要通过匹配某些条件将具有 10 个唯一值的 marketing_channel
列重新分类为 15 个不同的组。 我已经通过 case 语句完成了此操作,但输出位于 15 个新列中。
- 是否有更优雅的方法来重新分类 marketing_channel,只需添加 1 个额外的列(例如包含所有新的 15 个类的
"marketing_sub_channel"
)? - 有没有比创建 15 个案例语句更好的分类方法?正在考虑使用
with
子句,但这也会相当冗长
I need to re-categorise a column marketing_channel
with 10 unique values into 15 distinct groups by matching certain criteria.
I've done this via case statements but then the output is in 15 new columns.
- Is there a more elegant way to re-class the marketing_channel by simply adding 1 extra column like
"marketing_sub_channel"
that contains all new 15 classes? - Is there better way to do the classification than by creating 15 case statements? Was thinking a
with
clause, but that would also be quite lengthy
Output looks like this but ultimately just a single added column would be great:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
是的,您只需稍微更改一下格式即可。删除每行开头的“case”语句,然后将“End”放在语句末尾,如下所示:
或者在您的情况下:
另外我想指出,在您的 case 语句中,因为您有 '% display%' 和 '%_Displaybrand_global Progromatic Display%',您将较长且更具体的一个放在顶部,以便它可以在需要时触发。如果“%display%”位于顶部,那么它将始终首先触发,因为它包含另一个的子字符串。
Yes you just have to change the format a bit. Remove the "case" statement at the beginning of each line and just put the "End" at the end of the statement, like so :
or in your case:
Also I would like to note that in your case statement since you have '%display%' and '%_Display brand_global Progromatic Display%' that you place the longer more specific one on top so it can trigger if it needs to. If '%display%' is on top then it will always trigger first since it contains a substring of the other one.