---
title: "Selecting COUNT(*) with DISTINCT"  
description: "Selecting COUNT(*) with DISTINCT"  
author: "Ravi Misra"  
published: 2023-04-12  
updated: 2023-04-12  
canonical: https://answers.mindstick.com/qa/99388/selecting-count-with-distinct  
category: "database"  
tags: ["database"]  
reading_time: 3 minutes  

---

# Selecting COUNT(*) with DISTINCT

In [SQL Server](https://www.mindstick.com/articles/12343/sql-server-2017-ctp-2-0-now-available) 2005 I have a [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) `cm_production` that [lists](https://www.mindstick.com/articles/126273/books-commonly-found-on-college-reading-lists) all the [code](https://www.mindstick.com/articles/331918/why-is-code-optimization-important) that's been [put](https://www.mindstick.com/blog/369/introduction-to-the-asp-dot-net-web-api) into production. The table has a `ticket_number`, `program_type`, `program_name` and `push_number` along with some other columns.

GOAL: [Count](https://www.mindstick.com/forum/2295/how-to-count-number-of-visitors-for-website) all the [DISTINCT](https://www.mindstick.com/forum/158907/explain-the-purpose-of-the-distinct-keyword-in-sql-and-provide-an-example) [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) names by program type and [push](https://www.mindstick.com/news/2535/us-lawmakers-push-for-greater-oversight-of-elon-musk-s-brain-chip-company-neuralink) number.

What I have so far is:

```cs
DECLARE @push_number INT;
SET @push_number = [HERE_ADD_NUMBER];

SELECT DISTINCT COUNT(*) AS Count, program_type AS [Type]
FROM cm_production
WHERE push_number=@push_number
GROUP BY program_type
```

This gets me partway there, but it's counting all the program names, not the distinct ones (which I don't expect it to do in that [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver)). I guess I just can't wrap my head around how to tell it to count only the distinct program names without selecting them. Or something.

## Answers

### Answer by Jujhar Singh

To count the distinct program names by program type and push number in SQL [Server](https://www.mindstick.com/articles/710/apache-server-installation-on-windows) 2005, you can use the COUNT DISTINCT function along with a GROUP BY clause. Here is an example query that should achieve the desired result:

SQL

```plaintext
DECLARE @push_number INT;
SET @push_number = [HERE_ADD_NUMBER];

SELECT program_type AS [Type], push_number, COUNT(DISTINCT program_name) AS Count
FROM cm_production
WHERE push_number = @push_number
GROUP BY program_type, push_number;
```

In this query, we first declare and set the value of the variable **@push_number** to the desired push number. We then select the program type, push number, and the count of distinct program names for each combination of program type and push number.

The COUNT(DISTINCT) function is used to count the number of distinct program names for each group. By grouping the results by program type and push number using the GROUP BY clause, we ensure that the count is calculated for each unique combination of program type and push number.

Note that we include both program type and push number in the SELECT statement, as well as the GROUP BY clause. This is necessary to ensure that we get distinct counts for each combination of program type and push number.

This query should provide you with the desired result, showing the count of distinct program names for each program type and push number combination. If you need to filter the results further, you can add additional WHERE clauses or use the HAVING clause to filter the results based on aggregate functions.

It's important to note that using DISTINCT and GROUP BY clauses can impact the performance of your query, especially on large datasets. So, if performance is a concern, you may want to consider indexing or other optimization techniques to improve query speed.![Selecting COUNT(*) with DISTINCT](https://answers.mindstick.com/questionanswer/6880c310-88be-4882-862f-c691884d6caa/images/7efac045-ffd1-4479-97a9-30dd06c58f45.png)

In summary, by using the COUNT DISTINCT function along with the GROUP BY clause, you can count the distinct program names by program type and push number in SQL Server 2005.


---

Original Source: https://answers.mindstick.com/qa/99388/selecting-count-with-distinct

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
