Collections:
Using Group Functions in the ORDER BY Clause in SQL Server
Can Group Functions Be Used in the ORDER BY Clause in SQL Server?
✍: FYIcenter.com
If the query output is aggregated as groups, you can sort the groups by using group functions in the ORDER BY clause. The following statement returns the maximum "counts" in each group, determined by a unique combination of tag and year. The group output is sorted by the maximum "counts" in each group in ascending order:
SELECT tag, YEAR(created), MAX(counts) FROM fyi_links GROUP BY tag, YEAR(created) ORDER BY MAX(counts) GO tag year(created) max(counts) DEV 2006 120 DBA 2006 390 DEV 2004 439 SQA 2007 728 SQA 2003 828 DBA 2005 960 DBA 2007 972
2016-10-25, 726👍, 0💬
Popular Posts:
How to download and install Microsoft SQL Server Management Studio Express in SQL Server? Microsoft ...
Where to find MySQL database server tutorials? Here is a collection of tutorials, tips and FAQs for ...
What Is ISAM in MySQL? ISAM (Indexed Sequential Access Method) was developed by IBM to store and ret...
How To Calculate Age in Days, Hours and Minutes in SQL Server Transact-SQL? On many Web sites, news ...
Can You Create a View with Data from Multiple Tables in SQL Server? Can You Create a View with Data ...