need to convert data in multiple rows with same ID into 1 row with multiple columns

前端 未结 1 800
谎友^
谎友^ 2021-01-28 07:21

I reviewed versions of my question already addressed, but some of the good tips I found (using rank() over (partition...) for example, do not seem to work in the Sybase version

相关标签:
1条回答
  • 2021-01-28 07:38

    If you only have two preferences, then you can use min() and max():

    select email, min(preference) as preference1,
           (case when min(preference) <> max(preference) then max(preference) end) as preference2
    from t
    group by email;
    

    EDIT:

    If you have up to seven values, then pivot using row_number():

    select email,
           max(case when seqnum = 1 then preference end) as preference1,
           max(case when seqnum = 2 then preference end) as preference2,
           max(case when seqnum = 3 then preference end) as preference3,
           max(case when seqnum = 4 then preference end) as preference4,
           max(case when seqnum = 5 then preference end) as preference5,
           max(case when seqnum = 6 then preference end) as preference6,
           max(case when seqnum = 7 then preference end) as preference7
    from (select t.*, row_number() over (partition by email order by preference) as seqnum
          from t
         ) t
    group by email;
    

    EDIT II:

    You can actually do this with a correlated subquery instead of row_number():

    select email,
           max(case when seqnum = 1 then preference end) as preference1,
           max(case when seqnum = 2 then preference end) as preference2,
           max(case when seqnum = 3 then preference end) as preference3,
           max(case when seqnum = 4 then preference end) as preference4,
           max(case when seqnum = 5 then preference end) as preference5,
           max(case when seqnum = 6 then preference end) as preference6,
           max(case when seqnum = 7 then preference end) as preference7
    from (select t.*,
                 (select count(*)
                  from t t2
                  where t2.email = t.email and
                        t2.preference <= t.preference
                 ) as seqnum
          from t
         ) t
    group by email;
    
    0 讨论(0)
提交回复
热议问题