Collections:
Counting Groups Returned with the GROUP BY Clause in SQL Server
How To Count Groups Returned with the GROUP BY Clause in SQL Server?
✍: FYIcenter.com
If you use the COUNT(*) function on groups returned with the GROUP BY clause, it will count the number of rows within each group, not the number of groups. If you want to count the number of groups, you can put the GROUP BY query into a subquery and apply the COUNT(*) function on the main query as shown in the following tutorial exercise:
SELECT tag AS Category, YEAR(created) AS Year, COUNT(*) AS Counts FROM fyi_links GROUP BY tag, YEAR(created) GO Category Year Counts SQA 2003 1 DEV 2004 1 DBA 2005 1 DBA 2006 1 DEV 2006 1 DBA 2007 1 SQA 2007 1 SELECT COUNT(*) FROM ( SELECT tag AS Category, YEAR(created) AS Year, COUNT(*) AS Counts FROM fyi_links GROUP BY tag, YEAR(created) ) groups GO 7
⇒ "TOP" - Return the Top 5 Rows from a SELECT Query in SQL Server
⇐ Using Subqueries in the FROM Clause in SQL Server
⇑ Using SELECT Statements with Joins and Subqueries in SQL Server
2016-10-29, 1528🔥, 0💬
Popular Posts:
How to download and install Microsoft .NET Framework Version 2.0 in SQL Server? .NET Framework Versi...
How To Calculate DATETIME Value Differences Using the DATEDIFF() Function in SQL Server Transact-SQL...
How To Drop a Stored Procedure in Oracle? If there is an existing stored procedure and you don't wan...
How To Convert Numeric Values to Character Strings in MySQL? You can convert numeric values to chara...
What Are the Underflow and Overflow Behaviors on FLOAT Literals in SQL Server Transact-SQL? If you e...