{"id":1138,"date":"2013-08-22T10:40:00","date_gmt":"2013-08-22T14:40:00","guid":{"rendered":"http:\/\/deridder.ca\/blog\/?p=1138"},"modified":"2013-08-22T10:40:00","modified_gmt":"2013-08-22T14:40:00","slug":"partition-by","status":"publish","type":"post","link":"https:\/\/deridder.ca\/blog\/?p=1138","title":{"rendered":"PARTITION BY"},"content":{"rendered":"<p>I have often wanted to get the actual detail columns in a group by, that weren&#8217;t being grouped.. this is how<\/p>\n<p>Here\u2019s a quick summary of OVER and PARTITION BY (new in SQL 2005), for the uninitiated or forgetful\u2026<\/p>\n<p>OVER<br \/>\nOVER allows you to get aggregate information without using a GROUP BY. In other words, you can retrieve detail rows, and get aggregate data alongside it. For example, this query:<\/p>\n<p>SELECT SUM(Cost) OVER () AS Cost<br \/>\n, OrderNum<br \/>\nFROM Orders<\/p>\n<p>Will return something like this:<\/p>\n<p>Cost  OrderNum<br \/>\n10.00 345<br \/>\n10.00 346<br \/>\n10.00 347<br \/>\n10.00 348<\/p>\n<p>Quick translation:<\/p>\n<p>\u2022SUM(cost) \u2013 get me the sum of the COST column<br \/>\n\u2022OVER \u2013 for the set of rows\u2026.<br \/>\n\u2022() \u2013 \u2026that encompasses the entire result set.<br \/>\nOVER(PARTITION BY)<br \/>\nOVER, as used in our previous example, exposes the entire resultset to the aggregation\u2026\u201dCost\u201d was the sum of all [Cost]  in the resultset.  We can break up that resultset into partitions with the use of PARTITION BY:<\/p>\n<p>SELECT SUM(Cost) OVER (PARTITION BY CustomerNo) AS Cost<br \/>\n, OrderNum<br \/>\n, CustomerNo<br \/>\nFROM Orders<\/p>\n<p>My partition is by CustomerNo \u2013 each \u201cwindow\u201d of a single customer\u2019s orders will be treated separately from each other \u201cwindow\u201d\u2026.I\u2019ll get the sum of cost for Customer 1, and then the sum for Customer 2:<\/p>\n<p>Cost  OrderNum   CustomerNo<br \/>\n 8.00 345        1<br \/>\n 8.00 346        1<br \/>\n 8.00 347        1<br \/>\n 2.00 348        2<\/p>\n<p>The translation here is:<\/p>\n<p>\u2022SUM(cost) \u2013 get me the sum of the COST column<br \/>\n\u2022OVER \u2013 for the set of rows\u2026.<br \/>\n\u2022(PARTITION BY CustomerNo) \u2013 \u2026that have the same CustomerNo.<\/p>\n<p>&#8212;<br \/>\nNow I also wanted to perform what would be a &#8216;HAVING&#8217; clause on the group by.. couldn&#8217;t find anything yet, so just put a wrapper select statement with the added where clause<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have often wanted to get the actual detail columns in a group by, that weren&#8217;t being grouped.. this is how Here\u2019s a quick summary of OVER and PARTITION BY (new in SQL 2005), for the uninitiated or forgetful\u2026 OVER &hellip; <a href=\"https:\/\/deridder.ca\/blog\/?p=1138\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-1138","post","type-post","status-publish","format-standard","hentry","category-techie"],"_links":{"self":[{"href":"https:\/\/deridder.ca\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/deridder.ca\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/deridder.ca\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/deridder.ca\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/deridder.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1138"}],"version-history":[{"count":2,"href":"https:\/\/deridder.ca\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1138\/revisions"}],"predecessor-version":[{"id":1142,"href":"https:\/\/deridder.ca\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1138\/revisions\/1142"}],"wp:attachment":[{"href":"https:\/\/deridder.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deridder.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deridder.ca\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}