{"id":155,"date":"2005-10-06T01:16:19","date_gmt":"2005-10-06T01:16:19","guid":{"rendered":"http:\/\/www.bartbusschots.ie\/blog\/?p=155"},"modified":"2014-08-04T14:59:12","modified_gmt":"2014-08-04T14:59:12","slug":"a-distinct-quirk-in-postgresql","status":"publish","type":"post","link":"https:\/\/www.bartbusschots.ie\/s\/2005\/10\/06\/a-distinct-quirk-in-postgresql\/","title":{"rendered":"A DISTINCT quirk in PostgreSQL"},"content":{"rendered":"<p><\/p>\n<p>I was doing some NASTY SQL coding this evening (or squealing as <a href=\"http:\/\/www.minds.nuim.ie\/~provost\/GENU\/news\/index.php\" title=\"Smiler's (AKA provost's) BLog\">Smiler<\/a> would say) and I got bogged down quite a while trying to get <font face=\"courier new,courier,monospace\">DISTINCT<\/font> and <font face=\"courier new,courier,monospace\">ORDER BY<\/font> working in the same query. The <a href=\"http:\/\/www.postgresql.org\/\" title=\"PostgreSQL Home Page\">PostgreSQL<\/a> error message was not entirely clear and I must have misinterpreted it about 5 times before I finally figured out what it was so desperately trying to tell me!<\/p>\n<p>Turns out that even if you don&#8217;t actually want to select the field you are sorting on in a query that uses the <font face=\"courier new,courier,monospace\">DISTINCT<\/font> keyword you still HAVE to include it in your <font face=\"courier new,courier,monospace\">SELECT<\/font> list or PSQL will have kittens!<\/p>\n<p>BTW, I am aware that it is considered a &#8216;bad smell&#8217; by some hard-core SQL programmers to use to <font face=\"courier new,courier,monospace\">DISTINCT<\/font> key word and I generally agree with their logic (why go to the effort of selecting a load of extra rows that you are just gonna prune off in the end?). However, I can&#8217;t for the life of me figure out how to get rid of the <font face=\"courier new,courier,monospace\">DISTINCT<\/font> in the query below, any insights you may have would be appreciated (looks at Bon)!<\/p>\n<pre>SELECT DISTINCT a.id AS assignmentId, a.name AS assignmentName, a.team_id AS teamId, <br \/>  t.name AS teamName, a.completed AS assignmentComp, a.last_updated_at AS lastUpdated<br \/>FROM ((student_team st INNER JOIN teams t ON st.team_id = t.id) <br \/>  INNER JOIN assignments a ON t.id = a.team_id) <br \/>  INNER JOIN projects p ON t.project_id=p.id<br \/>WHERE a.completed='true'<br \/>ORDER BY a.last_updated_at DESC LIMIT 5;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was doing some NASTY SQL coding this evening (or squealing as Smiler would say) and I got bogged down quite a while trying to get DISTINCT and ORDER BY working in the same query. The PostgreSQL error message was not entirely clear and I must have misinterpreted it about 5 times before I finally [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[3,424],"series":[],"class_list":["post-155","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-databases","tag-postgresql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7t9xK-2v","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/comments?post=155"}],"version-history":[{"count":1,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/155\/revisions"}],"predecessor-version":[{"id":7411,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/155\/revisions\/7411"}],"wp:attachment":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/media?parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/categories?post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/tags?post=155"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/series?post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}