用于从一列中提取值的SQL查询,其中其他值列相同

时间:2014-04-01 08:46:17

标签: sql r postgresql

我有一个数据集

paper_author:

paper_id author_id
   1      521630
   1      972575
   1      1528710
   1      1611750
   2      1682088
   2      1589667
   2      972575 
   3      521630
   3      1589667

我需要获得所有作者的共同作者信息,这些作者是作者与作者一起撰写同一篇论文并因此按作者分组

author_id  co_authors
 521630     972575,1528710,1611750,1589667
 972575     521630,1528710,1611750,1589667
 .......

在author_id的第一行= 521630作者与author_id = 972575,1528710,1611750写了paper1和author_id = 1589667写了论文3.我写了一个R代码

co_author_id<-vector()
for(i in 1:length(paper_author))
{
  author_id_data<-paper_author[i,2]
  index1<-which(paper_author$author_id %in% author_id_data
  paper_ids<-paper_author$paper_id[index1]
  index2<-which(paper_author$paper_id %in% paper_ids)
  co_authors<-paper_author$author_id[index2]
  co_author_id[i]<-paste(co_authors,collapse=" ")
 }

但这是非常低效的,因为数据大小是1200万行,因此使用sql会很容易和很好。如何用sql完成

由于

2 个答案:

答案 0 :(得分:2)

这个问题有一个R标签,所以我假设需要R解决方案:

<强> sqldf

library(sqldf)
nr <- nrow(paper_author)
paper_author$seqno <- ave(1:nr, paper_author$paper_id, FUN = seq_along)


sqldf(c("create index i2 on paper_author(paper_id, seqno)",
    "select author_id, group_concat(coauthor) co_authors 
    from (
      select distinct A.author_id, C.author_id coauthor 
      from ( select * from main.paper_author where seqno = 1) A 
      left join (select * from main.paper_author where seqno > 1) C
      using (paper_id)
    ) group by author_id"))

<强> data.table

library(data.table)
dt <- data.table(paper_author, key = "paper_id")
dt[, seqno:=1:.N, by = paper_id]
m <- merge(dt[seqno == 1], dt[seqno > 1], all.x = TRUE, by = "paper_id")
unique(m[, list(author_id.x, author_id.y)])[, 
   list(co_authors = toString(author_id.y)), by = author_id.x]

<强> dplyr

library(dplyr)
gp <- paper_author %.% group_by(paper_id)
gp %.% 
  filter(row_number() == 1) %.%
  left_join( gp %.% filter(row_number() > 1), by = "paper_id" ) %.%
  ungroup() %.%
  select(author_id.x, author_id.y) %.%
  unique() %.%
  group_by(author_id.x) %.%
  summarise(co_authors = toString(author_id.y))

基础R

nr <- nrow(paper_author)
seqno <- ave(1:nr, paper_author$paper_id, FUN = seq_along)
m <- merge(paper_author[seqno == 1, ], 
           paper_author[seqno > 1, ],  all.x = TRUE, by = "paper_id")
u <- unique(m[c("author_id.x", "author_id.y")])
aggregate(list(co_authors = u$author_id.y), list(author = u$author_id.x), toString)

尝试使用以上内容:

paper_author <-
structure(list(paper_id = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L
), author_id = c(521630L, 972575L, 1528710L, 1611750L, 1682088L, 
1589667L, 972575L, 521630L, 1589667L), seqno = c(1L, 2L, 3L, 
4L, 1L, 2L, 3L, 1L, 2L)), .Names = c("paper_id", "author_id", 
"seqno"), row.names = c(NA, -9L), class = "data.frame")

已修订修改以使作者在输出中唯一。

答案 1 :(得分:1)

这就是我理解你的问题。 SQL Fiddle

select
    pa1.author_id,
    array_agg(pa2.author_id order by pa2.author_id) as co_author
from
    paper_author pa1
    left join
    paper_author pa2 on
        pa1.paper_id = pa2.paper_id
        and pa1.author_id != pa2.author_id
group by pa1.author_id
order by pa1.author_id