{"id":75,"date":"2009-02-24T10:30:36","date_gmt":"2009-02-24T08:30:36","guid":{"rendered":"http:\/\/www.crydust.be\/blog\/?p=75"},"modified":"2009-08-20T09:12:03","modified_gmt":"2009-08-20T07:12:03","slug":"selecting-a-random-employee-with-adjacency-tables","status":"publish","type":"post","link":"https:\/\/www.crydust.be\/blog\/2009\/02\/24\/selecting-a-random-employee-with-adjacency-tables\/","title":{"rendered":"Selecting a random employee with adjacency tables"},"content":{"rendered":"<p>In my <a href=\"http:\/\/www.crydust.be\/blog\/2009\/02\/24\/adjacency-tables\/\">previous blogpost<\/a> I mentioned a way to retrieve the entire tree from a hierarchy stored with adjacency tables. Now I&#8217;ve got a way to retrieve a random employee in an organisation.<\/p>\n<p>I use ORDER BY NEWID() to randomize the order and TOP 1 to get only one result.<\/p>\n<pre class=\"prettyprint\">\r\nWITH Bosses (id, boss_id, name, depth, hierarchy)\r\nAS (\r\n\tSELECT id\r\n\t, boss_id\r\n\t, name\r\n\t, 0\r\n\t, name\r\n\tFROM People\r\n\tWHERE boss_id IS NULL\r\n\t\r\n\tUNION ALL\r\n\t\r\n\tSELECT People.id\r\n\t, People.boss_id\r\n\t, People.name\r\n\t, Bosses.depth + 1\r\n\t, CONVERT(VARCHAR(50), Bosses.hierarchy + ' &gt; ' + People.name)\r\n\tFROM People\r\n\t, Bosses\r\n\tWHERE People.boss_id = Bosses.id\r\n)\r\nSELECT TOP 1 * \r\nFROM Bosses\r\nWHERE depth &gt;= 2\r\nORDER BY NEWID();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In my previous blogpost I mentioned a way to retrieve the entire tree from a hierarchy stored with adjacency tables. Now I&#8217;ve got a way to retrieve a random employee in an organisation. I use ORDER BY NEWID() to randomize &hellip; <a href=\"https:\/\/www.crydust.be\/blog\/2009\/02\/24\/selecting-a-random-employee-with-adjacency-tables\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-75","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/posts\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/comments?post=75"}],"version-history":[{"count":3,"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/posts\/75\/revisions\/141"}],"wp:attachment":[{"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/media?parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/categories?post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.crydust.be\/blog\/wp-json\/wp\/v2\/tags?post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}