sql查询将动态列转置为sybase中的行或不使用任何数据透视函数

时间:2018-06-21 11:11:30

标签: sql sybase-ase15

我对表格的输入

id  value
1    23
1    22
1    24
2    55
2    44

我的输出应该是

1 23|22|24
2 55|44

请帮助提供可以在sybase数据库中运行的查询。

1 个答案:

答案 0 :(得分:-1)

您可以使用row_number()进行此操作,该版本应在您的Sybase版本中可用:

select id,
       stuff((max(case when seqnum = 1 then '|' + value else '' end) +
              max(case when seqnum = 2 then '|' + value else '' end) +
              max(case when seqnum = 3 then '|' + value else '' end) +
              max(case when seqnum = 4 then '|' + value else '' end)
             ), 1, 1, '') as vals
from (select t.*, row_number() over (partition by id order by value) as seqnum
      from t
     ) t
group by id;
相关问题