SQL:将编码行存储为给定ID的单独列

时间:2017-08-15 18:54:50

标签: sql amazon-redshift

我有一个表,其中包含一组地址的地理编码数据。问题是纬度和经度存储在同一列中,但是由同一表中另一列中的代码(0或1)区分。每个地址都标有唯一的ID,该ID在列中显示两次(1表示lat,1表示长)。

我想在同一行显示lat和long以及相应的ID。

2 个答案:

答案 0 :(得分:0)

with ttt as (
            select 111 ID, '54.65548' coords, 1 def from dual
            union all
            select 111 ID, '-34.8456' coords, 0 def from dual
            )
select t1.ID, t1.coords, t2.coords
from ttt t1
    join ttt t2 ON t1.ID = t2.ID 
                   and t1.def <> t2.def 
                   and t1.def = 1 -- or t1.def = 0 if you want change order of coords

答案 1 :(得分:0)

    select unique_id, 
         max(case when code = 0 then geocoded_column end) as lat, 
         max(case when code = 1 then geocoded_column end) as long
    from geocoded_table
    group by unique_id