将行旋转到ID不重复的列

时间:2019-04-23 14:50:12

标签: sql sql-server

我有一些来自外部来源的数据,需要转到正确的列中。问题在于,id和另外一列没有对所有行重复。数据示例:

tidyr

所需的输出是

Rownumber ID     Type             Category Value
1         100   1-Purchase Order  Address  Cedar Rd
2                                 City     Beachwood
3                                 State    Ohio
4                                 Zip      44122
5         300   1-Purchase Order  Address  Rockside Rd
6                                 City     Independence
7                                 State    Ohio
8                                 Zip      44131
9         200   1-Purchase Order  Address  Rockside Rd
10                                City     Independence
11                                State    Ohio
12                                Zip      44131  

请注意,该ID以随机顺序出现,因此该代码不起作用:

Rownumber  ID   Type              Address      City        State  Zip
1         100  1-Purchase Order  Cedar Rd      Beachwood   Ohio   44122
2         300   1-Purchase Order Rockside Rd  Independence Ohio   44122
3         200   1-Purchase Order Rockside Rd  Independence Ohio   44122

1 个答案:

答案 0 :(得分:4)

您可以轻松修复此代码,因为您实际上不需要ID。您只需要一种定义组的方法。因此,累计计数应做相同的事情:

select row_number() over (order by min(rownumber)) as rownumber,
       max(id) as id,
       max(type) as type,
       max(case when category = 'Address' then value end) as address,
       max(case when category = 'City' then value end) as city,
       max(case when category = 'State' then value end) as state,
       max(case when category = 'Zip' then value end) as zip
from (select t.*,
             count(id) over (order by rownumber) as grp
      from t
     ) t
group by grp;

这会将所有NULL值分配给紧接在前的非null id分组。

Here是db <>小提琴。