Selecting COUNT(*) with DISTINCT
Eager to get count of unique entries? Use this:
This gives you a count of unique column_name values in table_name, shoving duplicates aside.
Distinct Counts Avoiding Asterisks
Never use COUNT(DISTINCT *). Use DISTINCT with a specific column for meaningful results. When dealing with distinct program types, here's the go-to statement:
It filters by push_number, groups by program_type, and counts distinct program_name.
Clear Results with Aliases and Filters
For output clarity, use aliases like AS Count for distinct counts, AS [Type] for field clarity. Don't overcomplicate act of filtering data using a variable like @push_number. It's as easy as pie.
Data is ordered by count then program type - prioritizing the meat and potatoes of the info.
Subqueries for Data Precision
When queries get complex, like counting distinct values in outer queries - subqueries or derived tables come to rescue:
Counts distinct program_name for a push_number like a boss - neatly packed in a subquery.
Squashing NULL Values
Use WHERE column_name IS NOT NULL to keep NULL values in check ensuring accurate counts. Just like in this piece of art:
This ensures that you're not counting phantom data. Ghostbusters would approve!
Adapting SQL Scripts
And when you'd want to count distinct regions and program types - a flexible SQL script is the way to go:
Here's to CONCAT for creating a unique identifier, and to your problem - goodbye!
Compatibility with Older SQL Versions
Working with SQL Server 2005? Some constructs are not supported. Replace CONCAT with the + operator for backward compatibility, and ISNULL for possible NULL values.
Was this article helpful?