简体   繁体   English

雅典娜:将列转换为不同的值

[英]Athena: Convert column to different values

I would like to convert a column to another value, kind of like having a map.我想将一列转换为另一个值,有点像拥有一张地图。 I am currently doing something like我目前正在做类似的事情

SELECT col1,
    col2,
    CASE
        data = 'data1' THEN 'd1'
        WHEN data = 'data2' THEN 'd2' ELSE data
    END AS converted_data
FROM some_table

Which works fine, but I really have a bunch of other possibilities, maybe 20 or more.这很好用,但我真的有很多其他的可能性,也许是 20 或更多。 It can potentially even get to hundreds in the future.未来它甚至可能达到数百个。

Is it possible to have the map as a table, and convert the values from that table?是否可以将地图作为表格,并转换该表格中的值? I believe you can use JOIN but that seems it might be pretty expensive?我相信你可以使用JOIN但这似乎很贵? Is there a way to use CASE without writing each statement and just basing the values from that table?有没有一种方法可以使用CASE而无需编写每条语句而仅基于该表中的值?

There really is not a way to use a case statement without writing each condition to do what you are trying to accomplish.如果不编写每个条件来完成您想要完成的事情,真的没有办法使用 case 语句。

If your end goal is a mapping to change the value of a column to something else and that could change or is a large number of items;如果您的最终目标是将列的值更改为其他内容的映射,并且可能会更改或者是大量项目; for easy maintenance you would want to use another table which then is going to require a join or the use of a sub-query.为了便于维护,您可能希望使用另一个需要连接或使用子查询的表。

Here are 2 working examples with test data to show how those work:这里有 2 个带有测试数据的工作示例来展示它们是如何工作的:

Left Join:左连接:

#CTEs with test data
with convert_data as (
    select *
    From (
            values('data1', 'd1'),
                ('data2', 'd2')
        ) as convertdata (ddatavalue, datareplace)
),
some_table as(
    select *
    From (
            values('1', '1', 'data1'),
                ('2', '2', 'data2'),
                ('3', '3', 'data3')
        ) as sametable (col1, col2, ddata)
)
#query showing how to do the join
select col1,
    col2,
    coalesce(datareplace, ddata) as converted_data
from some_table st
    left join convert_data cd on cd.ddatavalue = st.ddata

Sub-query:子查询:

#CTEs with test data
with convert_data as (
    select *
    From (
            values('data1', 'd1'),
                ('data2', 'd2')
        ) as convertdata (ddatavalue, datareplace)
),
some_table as(
    select *
    From (
            values('1', '1', 'data1'),
                ('2', '2', 'data2'),
                ('3', '3', 'data3')
        ) as sametable (col1, col2, ddata)
)
#query showing how to do the sub-query
select col1,
    col2,
    coalesce(
        (
            select cd.datareplace
            from convert_data cd
            where cd.ddatavalue = st.ddata
        ),
        ddata
    ) as converted_data
from some_table st

Which one will perform better over the other will really be dependent on your data, structure and data file format.哪一个会比另一个表现更好将取决于您的数据、结构和数据文件格式。 Test both and see which one works better for you.测试两者,看看哪一个更适合你。

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

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