{"id":118,"date":"2008-07-21T09:49:11","date_gmt":"2008-07-21T09:49:11","guid":{"rendered":"http:\/\/www.nostuff.org\/words\/?p=118"},"modified":"2008-07-21T09:49:11","modified_gmt":"2008-07-21T09:49:11","slug":"sql-grouping-mulitple-values-in-to-one-select-field","status":"publish","type":"post","link":"https:\/\/www.nostuff.org\/words\/2008\/sql-grouping-mulitple-values-in-to-one-select-field\/","title":{"rendered":"SQL grouping mulitple values in to one SELECT field"},"content":{"rendered":"<p>SQL is one of those things I always need to understand more than I do. Sometimes odd little problems can seem impossible with my limited understanding of the language.<\/p>\n<p>One such hit this me this morning. We wanted a report from our <a href=\"http:\/\/en.wikipedia.org\/wiki\/Institutional_repository\">Institutional Repository<\/a> (a online archive of the University&#8217;s research publications). The report was to include title, journal title, type (journal article, book, etc), author(s) and departments for each item in the report.<\/p>\n<p>Anyone with a basic understanding of relational databases will be able to picture the main table of publications, with another table for publication authors &#8211; one record for each author of each publication, with the publication&#8217;s id number to link them to the item in <a href=http:\/\/092.me>question<\/a>. The same set up for the departments associated with a particular item.<\/p>\n<p>With basic SQL, it would be easy to return a list which included multiple entries for items with multi authors, i.e. an item with three authors would be repeated three times in the list, each showing a different author. An item with three authors and two departments would be repeated six times to cover all combinations of authors and departments. Not very desirable. We could also provide a list which only shows each item once (as required) but only show one author and department (ie by using GROUP BY and perhaps a function to select the first author). Again would be less than ideal.<\/p>\n<p>A bit of googling showed <a href=\"http:\/\/archives.postgresql.org\/pgsql-sql\/1999-08\/msg00112.php\">creating an agregate function in postgress<\/a> (we are using mysql), and <a href=\"http:\/\/www.planet-source-code.com\/URLSEO\/vb\/Discussion\/AskAProShowPost!asp\/lngTopicId!30731\/TopicCategory!standards\/Flag!2\/lngWId!5\/blnURLSEO!true\/Posting.htm\">this solution which seems to use a tempory table<\/a>.<\/p>\n<p>The solution in the end was much more simpler, and involves using a function called <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/group-by-functions.html#function_group-concat\">group_concat<\/a>.<\/p>\n<p>The final SQL looked like this:<\/p>\n<pre>SELECT a.eprintid, a.type, a.title, a.publication,\nGROUP_CONCAT(c.creators_given, \" \", c.creators_family) as Authors,\nGROUP_CONCAT(d.depts) as Departments\nFROM `archive` a, `archive_creators` c, `archive_depts` d\nWHERE a.eprintid = c.eprintid\nAND a.eprintid = d.eprintid\nGROUP BY c.eprintid, d.eprintid<\/pre>\n<p>The two group_concats will produce a list of authors (and a list of departments) both comma separated. The author&#8217;s is a little more complex as the name is within two separate fields, though this isn&#8217;t a problem, we just need to specify both with a space in between to make them readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL is one of those things I always need to understand more than I do. Sometimes odd little problems can seem impossible with my limited understanding of the language. One such hit this me this morning. We wanted a report from our Institutional Repository (a online archive of the University&#8217;s research publications). The report was [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[59,169],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-techy","tag-eprints","tag-sql"],"_links":{"self":[{"href":"https:\/\/www.nostuff.org\/words\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nostuff.org\/words\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nostuff.org\/words\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nostuff.org\/words\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nostuff.org\/words\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":0,"href":"https:\/\/www.nostuff.org\/words\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nostuff.org\/words\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nostuff.org\/words\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nostuff.org\/words\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}