SQL group by aggregates Archives - Anuj Varma, Hands-On Technology Architect, Clean Air Activist https://www.anujvarma.com/tag/sql-group-by-aggregates/ Production Grade Technical Solutions | Data Encryption and Public Cloud Expert Tue, 14 Oct 2014 20:15:53 +0000 en-US hourly 1 https://wordpress.org/?v=7.0.2 https://www.anujvarma.com/wp-content/uploads/anujtech.png SQL group by aggregates Archives - Anuj Varma, Hands-On Technology Architect, Clean Air Activist https://www.anujvarma.com/tag/sql-group-by-aggregates/ 32 32 sql aggregates and group by https://www.anujvarma.com/sql-aggregates-and-group-by/ https://www.anujvarma.com/sql-aggregates-and-group-by/#respond Tue, 14 Oct 2014 20:14:47 +0000 http://www.anujvarma.com/?p=2709 When you select an aggregate (such as count (*), )  you also need to do a group by on the results. Select a.Name,count(b.title) as NumberOfBooks from AUTHOR a join BOOKS […]

The post sql aggregates and group by appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
When you select an aggregate (such as count (*), )  you also need to do a group by on the results.

Select a.Name,count(b.title) as NumberOfBooks from AUTHOR a join BOOKS b on a.ID = b.AuthorID group by a.Name   (this finds the NUMBER OF BOOKS written by each AUTHOR)

 

Side Note – Find the MAX occurrences of a  value from a list of possible values

To do an equivalent of a MAX function (from a list of values, find the value with the maximum occurrences), just do a group by as illustrated above, followed by an order by DESC

Select a.Name,count(b.title) as NumberOfBooks from AUTHOR a join BOOKS b on a.ID = b.AuthorID group by a.Name order by NumberOfBooks DESC  (This ORDERS The NUMBER Of BOOKS in DESCENDING ORDER, so the MAX is on top).

The post sql aggregates and group by appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
https://www.anujvarma.com/sql-aggregates-and-group-by/feed/ 0