电影镜头数据具有非规范化类型:
movie1 ACTION
movie1 ADVENTURE
movie2 ACTION
...
是否可以编写一个简单的SQL来获取规范化的电影类型:
library(networkD3)
library(data.table)
set.seed(1999)
links <- data.table(
src = rep(0:4, times=c(1,1,2,3,5)),
target = sample(1:11, 12, TRUE),
value = sample(100, 12)
)[src < target, ] # no loops
nodes <- data.table(name=LETTERS[1:12])
## Need to hover to get counts
sankeyNetwork(Links=links, Nodes=nodes, Source='src', Target='target',
Value='value', NodeID='name', fontSize=16)
## Add text to label
txt <- links[, .(total = sum(value)), by=c('target')]
nodes[txt$target+1L, name := paste0(name, ' (', txt$total, ')')]
## Displays the counts as part of the labels
sankeyNetwork(Links=links, Nodes=nodes, Source='src', Target='target',
Value='value', NodeID='name', fontSize=16, width=600, height=300)
假设我在MySQL或PostgreSQL中这样做。
答案 0 :(得分:2)
对于PostgreSQL
,您可以将unnest
与string_to_array
:
select name, unnest(string_to_array(genres, '|'))
from movies;