使用键列将行数据转换为单列

问题描述

我想使用sql或R将原始行数据中的键列转换为单行数据。

示例行:

---------------------------------------------------------------------
| Customer #  | Item1   |   Item2   |   Item3  |  Item4 |
---------------------------------------------------------------------
| 1111111     | A12345  |   C45567  |   G34589 | A34529 |
---------------------------------------------------------------------

这是我想要的结果:

   -------------------------------
   | Customer # | Columnname | Value    |
   -------------------------------
   | 1111111    | Item1      | A12345   |
   | 1111111    | Item2      | C45567   |
   | 1111111    | Item3      | G34589   |
   | 1111111    | Item4      | A34529   |

解决方法

您要取消透视表中的列为行。

您标记了问题mysqli,所以让我假设您正在使用MySQL。在此数据库中,您可以使用union all

select customer,'Item1' columname,Item1 value from mytable
union all select customer,'Item2',Item2 from mytable
union all select customer,'Item3',Item3 from mytable
union all select customer,'Item4',Item4 from mytable

其他数据库具有更整洁的解决方案,通常使用横向联接和values()。以Postgres为例:

select t.customer,x.columname,x.value
from mytable t
cross join lateral (values 
    ('Item1',Item1),('Item2',Item2),('Item3',Item3),('Item4',Item4)
) x(columname,value)

在SQL Server中,只需将cross join lateral替换为cross apply

,

在R中,您可以按以下方式使用dplyr和tidyr。

library(dplyr)
library(tidyr)

data <- tibble(customer = c(11111),item1 = c('sfdhdshv'),item2 = c('dfh'),item3 = c('kjg'))

data

#   customer item1    item2 item3
#      <dbl> <chr>    <chr> <chr>
# 1    11111 sfdhdshv dfh   kjg 

data %>%
  pivot_longer(!customer,names_to = 'columnname',values_to = 'value')

#   customer columnname value   
#      <dbl> <chr>      <chr>   
# 1    11111 item1      sfdhdshv
# 2    11111 item2      dfh     
# 3    11111 item3      kjg