2014-07-03 68 views
0

我在我的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 | 

,從我想爲每一個作者對於給定的紙附加列「共同作者」的條目 - 列出用空格分隔所有的合作者,類似(但每個作者):

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 

編輯

好了,現在我有一個查詢,這將創建一個跨產品的每個AU雷神從列和創建的合作者列,但我仍然需要從合作者列

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 
+0

檢查我最近的答案http://stackoverflow.com/a/24432345/131874 –

+1

你怎麼知道作者是誰以及合着者是誰? –

+0

我想用解決方案命令之前的行數創建一個結果。看看我更新的問題。 – Patryk

回答

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)。

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; 
+0

我希望在運行該命令之前有多少行,使每個共同作者在該論文的作者列中都有。看看我更新的問題。 – Patryk

+0

該解決方案將作者姓名與分隔符分開,因此存在S. R. Duc'作爲作者,它將被分成「S」作爲答案。 – Patryk

相關問題