如何为组中的每个元素添加新列到元组?

时间:2014-07-03 01:00:27

标签: sql postgresql group-by

我的postgres数据库中有以下数据集:

 paperid | authorid |         name         |  
---------+----------+----------------------+
  896452 |     4976 | S. R. Duc            | 
  896452 |   186921 | F. Kolo              | 
  896452 |   318493 | C. D. Becker         | 
  896452 |   615968 | V. B. Duthon         | 
  896452 |   823663 | C. C. Charbonnier    | 
  896452 |   833091 | P. Hoffmeyer         | 
  896452 |   843054 | C. W. A. Pfirrmann   | 
  896452 |  1058596 | J. Menetrey          | 
  896452 |  2033639 | N. Magnenat-Thalmann | 

由此我想为给定论文的每位作者创建一个条目,附加列“Coauthors” - 列出所有由空格分隔的共同作者,如(但对于每个作者):

 paperid |   author          |                                                         coauthors                                                             
---------+-------------------+------------------------------------------------------------------------------------------------------------
  896452 | S. R. Duc         | C. C. Charbonnier C. D. Becker C. W. A. Pfirrmann F. Kolo J. Menetrey N. Magnenat-Thalmann P. Hoffmeyer V. B. Duthon
  896452 | C. C. Charbonnier | S. R. Duc  C. D. Becker C. W. A. Pfirrmann F. Kolo J. Menetrey N. Magnenat-Thalmann P. Hoffmeyer V. B. Duthon
... etc for each coauthor

编辑

好的,现在我有一个查询,它将为列中的每个作者和创建的coauthors列创建一个交叉产品,但我仍然需要从coauthors列中删除单个作者

SELECT foo.paperid, npa.name, foo.authors FROM newpaperauthor npa CROSS JOIN (select paperid, string_agg(name, ' ')  as authors                                
FROM newpaperauthor 
GROUP BY paperid 
ORDER BY paperid) foo ;
 paperid |         name         |                                                            authors                                                             
---------+----------------------+--------------------------------------------------------------------------------------------------------------------------------
  896452 | S. R. Duc            | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | F. Kolo              | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | C. D. Becker         | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | V. B. Duthon         | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | C. C. Charbonnier    | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | P. Hoffmeyer         | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | C. W. A. Pfirrmann   | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | J. Menetrey          | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | N. Magnenat-Thalmann | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann

2 个答案:

答案 0 :(得分:0)

我假设主要作者在authorid组中至少paperid

SELECT PAPERID, 
       SPLIT_PART(STRING, ' ', 1) AS AUTHOR, 
       RIGHT( STRING, LENGTH(STRING) - LENGTH(SPLIT_PART(STRING, ' ', 1))-1 ) AS COAUTHORS
 FROM (
       SELECT PAPERID, 
              STRING_AGG(NAME, ' ' ORDER BY AUTHORID)  AS STRING
         FROM (
               SELECT * 
                 FROM PAPERWORK 
                WHERE PAPERID='896452'
              ) AS TEMP1
        GROUP BY PAPERID
      ) AS TEMP2;

答案 1 :(得分:0)

好的,所以我最终得到了这个(至少对于只有一个paperid的表来说):

SELECT  foo.paperid, npa.name, foo.authors INTO npatest FROM newpaperauthor npa CROSS JOIN (select paperid, string_agg(name, ' ')  as authors 
FROM newpaperauthor 
GROUP BY paperid 
ORDER BY paperid) foo ; 
UPDATE npatest SET authors = regexp_replace(authors, concat(name, ' '), ''); 
SELECT * FROM npatest;

SELECT 9
UPDATE 9
 paperid |         name         |                                                            authors                                                             
---------+----------------------+--------------------------------------------------------------------------------------------------------------------------------
  896452 | S. R. Duc            | F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | F. Kolo              | S. R. Duc C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | C. D. Becker         | S. R. Duc F. Kolo V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | V. B. Duthon         | S. R. Duc F. Kolo C. D. Becker C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | C. C. Charbonnier    | S. R. Duc F. Kolo C. D. Becker V. B. Duthon P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | P. Hoffmeyer         | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann
  896452 | C. W. A. Pfirrmann   | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer J. Menetrey N. Magnenat-Thalmann
  896452 | J. Menetrey          | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann N. Magnenat-Thalmann
  896452 | N. Magnenat-Thalmann | S. R. Duc F. Kolo C. D. Becker V. B. Duthon C. C. Charbonnier P. Hoffmeyer C. W. A. Pfirrmann J. Menetrey N. Magnenat-Thalmann

我现在将尝试找到整个表的解决方案(包含许多paperid s)。