我在我的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
檢查我最近的答案http://stackoverflow.com/a/24432345/131874 –
你怎麼知道作者是誰以及合着者是誰? –
我想用解決方案命令之前的行數創建一個結果。看看我更新的問題。 – Patryk