DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS   SQL 2014 PROGRAMMING   DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
How to get frequency for a subset of first names?

Execute the following T-SQL example scripts in Microsoft SQL Server Management Studio Query Editor to demonstrate how to obtain frequency only for a subset of first names.

-- SQL common table expression - t-sql cte - MSSQL group by count
USE AdventureWorks;
WITH cteFirstNameFrequency
     AS (SELECT   FirstName,
                  NameFrequency = count(* )
         FROM     Person.Contact
         WHERE    FirstName IN ('Peter','Jane','Kathleen','John')
         GROUP BY FirstName
         UNION
         SELECT   FirstName,
                  NameFrequency = 0
         FROM     Person.Contact
         WHERE    FirstName NOT IN ('Peter','Jane','Kathleen','John')
         GROUP BY FirstName)
SELECT   FirstName,
         NameFrequency
FROM     cteFirstNameFrequency
ORDER BY NameFrequency DESC,
         FirstName
GO
/* Partial results
 
FirstName   NameFrequency
John        58
Peter       28
Kathleen    22
Jane        5
A.          0
A. Scott    0
Aaron       0
Abby        0

*/

------------
 

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE