{"id":429,"date":"2021-11-11T11:43:21","date_gmt":"2021-11-11T11:43:21","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/?page_id=429"},"modified":"2021-11-17T13:07:09","modified_gmt":"2021-11-17T13:07:09","slug":"aggregate-functions","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/higher\/aggregate-functions\/","title":{"rendered":"Aggregate Functions and GROUP BY"},"content":{"rendered":"<p><strong>Examples are based on the <a href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/reference\/sample-databases\/\">GoGo Gadgets<\/a> database<\/strong><\/p>\n<p>Aggregate Functions are calculations applied to all the records found from a SELECT command.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">AVG(xxx) -- return the average value of specified field or expression\r\nMAX(xxx) -- return largest value for specified field or expression\r\nMIN(xxx) -- return smallest value for specified field or expression\r\nSUM(xxx) -- return the total for specified field or expression\r\nCOUNT()  -- return number of records found<\/pre>\n<p>Example:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">SELECT MIN(price), MAX(price)\r\nFROM item<\/pre>\n<table class=\"datatable\">\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>MIN(price)<\/strong><\/td>\n<td><strong>MAX(price)<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td>2.0<\/td>\n<td>39.0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Aliases<\/h2>\n<p>Aliases can be used to give more readable headings for columns.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">SELECT MIN(price) AS &quot;Cheapest Price&quot;, \r\n    MAX(price) AS &quot;Dearest Price&quot;\r\nFROM item;<\/pre>\n<table class=\"datatable\">\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>Cheapest Price<\/strong><\/td>\n<td><strong>Dearest Price<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td>2.0<\/td>\n<td>39.0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Aggregate and Non-Aggregate Expressions<\/h2>\n<p>Mixing non-aggregate and aggregate expressions in a SELECT statement is not permitted without Grouping<\/p>\n<p>The query below is illegal, as it mixes non-aggregate productName with the aggregate function MIN &#8211; it does not return the name of the products with the minumum price, it returns the name of the first product that matches the minimum price.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">SELECT itemID, description, MIN(price)\r\nFROM item<\/pre>\n<table class=\"datatable\">\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>ItemID<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<td><strong>MIN(price)<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td>868<\/td>\n<td>Screen Wipes<\/td>\n<td>2.0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Calculations and Other Functions<\/h2>\n<p>Aggregating functions can use other calculations:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">SELECT MAX(price*quantity) \r\nFROM OrderItem, Item \r\nWHERE OrderItem.ItemID = Item.ItemID<\/pre>\n<table class=\"datatable\">\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>MAX(price*quantity)<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td>88.0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Other functions can be used to modify the results of aggregate functions:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">SELECT ROUND(AVG(price),2) AS &quot;Average Item Price&quot;\r\nFROM item;<\/pre>\n<table class=\"datatable\">\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>Average Item Price<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td>13.7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>GROUP BY<\/h2>\n<p>Aggregate functions return single rows of values, as in the examples above.<\/p>\n<p>Groups are used with aggregate functions to get results for each group. The SELECT clause should include a non-aggregate expression (the name of the group) to label each group:<\/p>\n<p>Example: Find the average price for each item category:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">SELECT category, ROUND(AVG(price),2) AS &quot;Average Price&quot;\r\nFROM item\r\nGROUP BY category;<\/pre>\n<table class=\"datatable\">\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>Category<\/strong><\/td>\n<td><strong>Average Price<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td>Boys Toys<\/td>\n<td>14.1<\/td>\n<\/tr>\n<tr>\n<td><strong>2<\/strong><\/td>\n<td>Computer Accessories<\/td>\n<td>13.0<\/td>\n<\/tr>\n<tr>\n<td><strong>3<\/strong><\/td>\n<td>Girls Gizmos<\/td>\n<td>11.29<\/td>\n<\/tr>\n<tr>\n<td><strong>4<\/strong><\/td>\n<td>Office Distractions<\/td>\n<td>10.89<\/td>\n<\/tr>\n<tr>\n<td><strong>5<\/strong><\/td>\n<td>Personal Grooming<\/td>\n<td>18.0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The GROUP BY clause must be after any WHERE clause, and before any ORDER BY clause:<\/p>\n<pre class=\"brush: sql; highlight: [3,5]; title: ; notranslate\" title=\"\">SELECT category, ROUND(AVG(price),2) AS &quot;Average Price&quot;\r\nFROM item\r\nWHERE category NOT LIKE &quot;%toys%&quot;\r\nGROUP BY category\r\nORDER BY ROUND(AVG(price),2) DESC<\/pre>\n<table class=\"datatable\">\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>Category<\/strong><\/td>\n<td><strong>Average Price<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td>Personal Grooming<\/td>\n<td>18.0<\/td>\n<\/tr>\n<tr>\n<td><strong>2<\/strong><\/td>\n<td>Computer Accessories<\/td>\n<td>13.0<\/td>\n<\/tr>\n<tr>\n<td><strong>3<\/strong><\/td>\n<td>Girls Gizmos<\/td>\n<td>11.29<\/td>\n<\/tr>\n<tr>\n<td><strong>4<\/strong><\/td>\n<td>Office Distractions<\/td>\n<td>10.89<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Grouping can be applied to single fields, or multiple fields.<\/p>\n<p>Example: find total sales for each city\/region and category for orders that have not been dispatched:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">SELECT Address3 as &quot;City\/Region&quot;, Category, SUM(price*quantity) AS &quot;Sales Pending&quot;\r\nFROM Customer c, CustOrder co, OrderItem oi, Item i\r\nWHERE c.CustomerID = co.CustomerID\r\nAND co.OrderNo = oi.OrderNo\r\nAND oi.ItemID = i.ItemID\r\nAND orderDispatched = &quot;&quot;\r\nGROUP BY Address3, Category<\/pre>\n<table class=\"datatable\">\n<tbody>\n<tr>\n<td><\/td>\n<td><strong>City\/Region<\/strong><\/td>\n<td><strong>Category<\/strong><\/td>\n<td><strong>Sales Pending<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>1<\/strong><\/td>\n<td>Aberdeen<\/td>\n<td>Boys Toys<\/td>\n<td>4.0<\/td>\n<\/tr>\n<tr>\n<td><strong>2<\/strong><\/td>\n<td>Aberdeen<\/td>\n<td>Computer Accessories<\/td>\n<td>78.0<\/td>\n<\/tr>\n<tr>\n<td><strong>3<\/strong><\/td>\n<td>Aberdeen<\/td>\n<td>Girls Gizmos<\/td>\n<td>12.0<\/td>\n<\/tr>\n<tr>\n<td><strong>4<\/strong><\/td>\n<td>Aberdeen<\/td>\n<td>Personal Grooming<\/td>\n<td>30.0<\/td>\n<\/tr>\n<tr>\n<td><strong>5<\/strong><\/td>\n<td>Edinburgh<\/td>\n<td>Boys Toys<\/td>\n<td>14.0<\/td>\n<\/tr>\n<tr>\n<td><strong>6<\/strong><\/td>\n<td>Edinburgh<\/td>\n<td>Computer Accessories<\/td>\n<td>14.0<\/td>\n<\/tr>\n<tr>\n<td><strong>7<\/strong><\/td>\n<td>Glasgow<\/td>\n<td>Computer Accessories<\/td>\n<td>22.0<\/td>\n<\/tr>\n<tr>\n<td><strong>8<\/strong><\/td>\n<td>Glasgow<\/td>\n<td>Girls Gizmos<\/td>\n<td>32.0<\/td>\n<\/tr>\n<tr>\n<td><strong>9<\/strong><\/td>\n<td>Glasgow<\/td>\n<td>Personal Grooming<\/td>\n<td>34.0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Examples are based on the GoGo Gadgets database Aggregate Functions are calculations applied to all the records found from a SELECT command. AVG(xxx) &#8212; return the average value of specified field or expression MAX(xxx) &#8212; return largest value for specified field or expression MIN(xxx) &#8212; return smallest value for specified field or expression SUM(xxx) &#8212; return the total for specified field or expression COUNT() &#8212; return number of records found<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/higher\/aggregate-functions\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":417,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-429","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/comments?post=429"}],"version-history":[{"count":23,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/429\/revisions"}],"predecessor-version":[{"id":602,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/429\/revisions\/602"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/pages\/417"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingsql\/wp-json\/wp\/v2\/media?parent=429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}