577

In SQL Server 2005 I have a table cm_production that lists all the code that's been put into production. The table has a ticket_number, program_type, program_name and push_number along with some other columns.

GOAL: Count all the DISTINCT program names by program type and push number.

What I have so far is:

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). 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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
somacore
  • 6,294
  • 3
  • 24
  • 19

8 Answers8

971

Count all the DISTINCT program names by program type and push number

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

DISTINCT COUNT(*) will return a row for each unique count. What you want is COUNT(DISTINCT <expression>): evaluates expression for each row in a group and returns the number of unique, non-null values.

Liam
  • 27,717
  • 28
  • 128
  • 190
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • Unique != distinct. Distinct means values that are different from other values. It can have duplicates, which we can remove by using DISTINCT. Unique value on the other hand exists only once, they have no duplicates. – Paweł Szmajda Sep 26 '22 at 15:18
177

I needed to get the number of occurrences of each distinct value. The column contained Region info. The simple SQL query I ended up with was:

SELECT Region, count(*)
FROM item
WHERE Region is not null
GROUP BY Region

Which would give me a list like, say:

Region, count
Denmark, 4
Sweden, 1
USA, 10
Netsi1964
  • 3,244
  • 1
  • 27
  • 17
  • hey @Netsi1964 same query is used but I want Region, State, Count, it can be possible? Please help me –  Oct 31 '17 at 09:07
92

You have to create a derived table for the distinct columns and then query the count from that table:

SELECT COUNT(*) 
FROM (SELECT DISTINCT column1,column2
      FROM  tablename  
      WHERE condition ) as dt

Here dt is a derived table.

Grzegorz Smulko
  • 2,525
  • 1
  • 29
  • 42
venkatesh
  • 921
  • 6
  • 2
  • 1
    Thank you! I've used a lot of SQL in my life across a lot of databases, and this is the first time I had to qualify it as a temp table with "as X". – Mmm Oct 16 '15 at 02:50
  • 9
    Note that the normal terminology for "dt" here is _derived_ table, not temp table – 8forty Feb 02 '17 at 21:48
25
SELECT COUNT(DISTINCT program_name) AS Count, program_type AS [Type] 
FROM cm_production 
WHERE push_number=@push_number 
GROUP BY program_type
van
  • 74,297
  • 13
  • 168
  • 171
17

try this:

SELECT
    COUNT(program_name) AS [Count],program_type AS [Type]
    FROM (SELECT DISTINCT program_name,program_type
              FROM cm_production 
              WHERE push_number=@push_number
         ) dt
    GROUP BY program_type
KM.
  • 101,727
  • 34
  • 178
  • 212
2

You can try the following query.

SELECT column1,COUNT(*) AS Count
FROM tablename where createddate >= '2022-07-01'::date group by column1
Manoj Tarkar
  • 438
  • 4
  • 9
0

To count distinct program names by program type and push number in SQL Server 2005, you can use a subquery to first select the distinct program names, and then perform the count in the outer query. Here's an example query that should give you the desired result:

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

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

In this query, the subquery selects the distinct program names and program types from the cm_production table based on the specified push_number. Then, in the outer query, you perform the count on the distinct program names and group the results by program type.

By the way, with dbForge Studio for SQL Server, you can easily write, execute, and analyze your SQL queries.

-1

This is a good example where you want to get count of Pincode which stored in the last of address field

SELECT DISTINCT
    RIGHT (address, 6),
    count(*) AS count
FROM
    datafile
WHERE
    address IS NOT NULL
GROUP BY
    RIGHT (address, 6)
Faisal
  • 4,591
  • 3
  • 40
  • 49