Selecting COUNT(*) with DISTINCT
Asked Answered
E

7

598

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.

Enunciation answered 5/10, 2009 at 18:19 Comment(0)
M
1003

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.

Mostly answered 5/10, 2009 at 18:26 Comment(1)
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.Coastguardsman
P
186

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
Plagio answered 12/8, 2014 at 19:3 Comment(1)
hey @Plagio same query is used but I want Region, State, Count, it can be possible? Please help meDanner
Z
99

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.

Zolazoldi answered 30/4, 2015 at 7:9 Comment(2)
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".Cohbath
Note that the normal terminology for "dt" here is derived table, not temp tableParipinnate
P
27
SELECT COUNT(DISTINCT program_name) AS Count, program_type AS [Type] 
FROM cm_production 
WHERE push_number=@push_number 
GROUP BY program_type
Pinfish answered 5/10, 2009 at 18:27 Comment(1)
distinct has to go inside the ( ) of the count, thanksNonresistance
A
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
Arithmetic answered 5/10, 2009 at 18:25 Comment(0)
K
1

You can try the following query.

SELECT column1,COUNT(*) AS Count
FROM tablename where createddate >= '2022-07-01'::date group by column1
Kimberelykimberlee answered 13/7, 2022 at 6:18 Comment(0)
P
-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)
Pagurian answered 24/6, 2016 at 5:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.