Can Group Functions Be Used in the ORDER BY Clause in MS SQL Server?
Submitted by: AdministratorIf 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 ggl_links GROUP BY tag, YEAR(created)
ORDER BY MAX(counts)
GO<pre>
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</pre>
Submitted by: Administrator
SELECT tag, YEAR(created), MAX(counts)
FROM ggl_links GROUP BY tag, YEAR(created)
ORDER BY MAX(counts)
GO<pre>
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</pre>
Submitted by: Administrator
Read Online MS SQL Server Job Interview Questions And Answers
Top MS SQL Server Questions
☺ | How To Start SQL Server Browser Service? |
☺ | What Are the Requirements on SQL Server Network Connections? |
☺ | How to download Microsoft SQL Server 2005 Express Edition? |
☺ | How To Execute the Cursor Queries with "OPEN" Statements? |
☺ | What Happens If Strings Are Casted into Wrong Code Pages in MS SQL Server? |
Top Databases Programming Categories
☺ | RDBMS Interview Questions. |
☺ | SQL Interview Questions. |
☺ | SSRS Interview Questions. |
☺ | Database Administrator (DBA) Interview Questions. |
☺ | Sybase Interview Questions. |