{"id":1028,"date":"2007-12-07T14:04:09","date_gmt":"2007-12-07T22:04:09","guid":{"rendered":"http:\/\/cephas.net\/blog\/2007\/12\/07\/sql-getting-the-count-of-the-result-of-a-derived-table\/"},"modified":"2007-12-07T14:11:23","modified_gmt":"2007-12-07T22:11:23","slug":"sql-getting-the-count-of-the-result-of-a-derived-table","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2007\/12\/07\/sql-getting-the-count-of-the-result-of-a-derived-table\/","title":{"rendered":"SQL: getting the count of the result of a derived table"},"content":{"rendered":"<p>Recording this for posterity: let&#8217;s say you&#8217;re working on a store and you&#8217;ve got a database table that stores orders and a database table that stores customers and that you want to get a count of all the customers who&#8217;ve ordered more than 10 times.  You&#8217;re going to write a query that looks like this:<\/p>\n<pre>SELECT customerID FROM orders GROUP BY customerID HAVING COUNT(*) > 10<\/pre>\n<p>to get a list of all the customerID&#8217;s who&#8217;ve ordered more than ten times and a query like this:<\/p>\n<pre>SELECT COUNT(*) FROM (SELECT customerID FROM orders GROUP BY customerID HAVING COUNT(*) > 10) as n<\/pre>\n<p>to get a count of those customers. Just sayin&#8217;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recording this for posterity: let&#8217;s say you&#8217;re working on a store and you&#8217;ve got a database table that stores orders and a database table that stores customers and that you want to get a count of all the customers who&#8217;ve ordered more than 10 times. You&#8217;re going to write a query that looks like this: &hellip; <a href=\"https:\/\/cephas.net\/blog\/2007\/12\/07\/sql-getting-the-count-of-the-result-of-a-derived-table\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">SQL: getting the count of the result of a derived table<\/span> <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":[],"categories":[2,36],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/1028"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=1028"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/1028\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=1028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=1028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=1028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}