SQL Server 2005: Counting Unique Program Names by Type and Push Number
This guide demonstrates how to efficiently count distinct program names, categorized by program type and a given push number, within SQL Server 2005. A common mistake is overlooking the need for distinct counting.
The COUNT(DISTINCT <expression>)
function is the key to accurate results. This function counts only unique, non-null values of the specified expression within each group.
Here's the corrected SQL query:
<code class="language-sql">SELECT program_type AS [Type], COUNT(DISTINCT program_name) AS [Count] FROM cm_production WHERE push_number = @push_number GROUP BY program_type</code>
This query effectively counts distinct program names for each program type, filtered by the provided @push_number
parameter. The output clearly shows the desired counts for each program type.
The above is the detailed content of How to Count Distinct Program Names by Type and Push Number in SQL Server 2005?. For more information, please follow other related articles on the PHP Chinese website!