Selecting COUNT(*) with DISTINCT - sql

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.

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.

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

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.

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

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

You can try the following query.
SELECT column1,COUNT(*) AS Count
FROM tablename where createddate >= '2022-07-01'::date group by column1

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)

Related

PostgreSQL create count, count distinct columns

fairly new to PostgreSQL and trying out a few count queries. I'm looking to count and count distinct all values in a table. Pretty straightforward -
CountD Count
351 400
With a query like this:
SELECT COUNT(*)
COUNT(id) AS count_id,
COUNT DISTINCT(id) AS count_d_id
FROM table
I see that I can create a single column this way:
SELECT COUNT(*) FROM (SELECT DISTINCT id FROM table) AS count_d_id
But the title (count_d_id) doesn't come through properly and unsure how can I add an additional column. Guidance appreciated
This is the correct syntax:
SELECT COUNT(id) AS count_id,
COUNT(DISTINCT id) AS count_d_id
FROM table
Your original query aliases the subquery rather than the column. You seem to want:
SELECT COUNT(*) AS count_d_id FROM (SELECT DISTINCT id FROM table) t
-- column alias --^ -- subquery alias --^

Filter by number of occurrences in a SQL Table

Given the following table where the Name value might be repeated in multiple rows:
How can we determine how many times a Name value exists in the table and can we filter on names that have a specific number of occurrances.
For instance, how can I filter this table to show only names that appear twice?
You can use group by and having to exhibit names that appear twice in the table:
select name, count(*) cnt
from mytable
group by name
having count(*) = 2
Then if you want the overall count of names that appear twice, you can add another level of aggregation:
select count(*) cnt
from (
select name
from mytable
group by name
having count(*) = 2
) t
It sounds like you're looking for a histogram of the frequency of name counts. Something like this
with counts_cte(name, cnt) as (
select name, count(*)
from mytable
group by name)
select cnt, count(*) num_names
from counts_cte
group by cnt
order by 2 desc;
You need to use a GROUP BY clause to find counts of name repeated as
select name, count(*) AS Repeated
from Your_Table_Name
group by name;
If You want to show only those Which are repeated more than one times. Then use the below query which will show those occurrences which are there more than one times.
select name, count(*) AS Repeated
from Your_Table_Name
group by name having count(*) > 1;

Select statement with count

I want to count the number of ABC group using id.
SELECT group, count(id) as total FROM `user` WHERE group=`ABC`;
What's wrong?
Many thanks.
Include the columns in the select list in group by clause when using aggregate functions.
SELECT group, count(id) as total FROM user
WHERE group=`ABC`
GROUP BY group
Else simply get the count with out using other columns in the select statement.
SELECT count(id) as total FROM user
WHERE group=`ABC`
Try this:
SELECT group, count(id) as total FROM `user`
group by group having group like 'ABC';
If you want to get COUNT of users, who has the "group" field = "ABC"
SELECT count(id) as total FROM user WHERE group='ABC';
Also, it's better to avoid using SQL keywords in column names (GROUP is an SQL keyword)

How to Compare Column to Count in a Query

I'm not too sophisticated with SQL, I don't know how to compare a count back to a column I'm selecting in a Query in Access. One column is generated as a count that counts IDs, but I want to verify it's correct (compare the column GroupCount with a SQL Count clause)
Here is what I have at the moment:
SELECT GroupID, GroupCount, COUNT(*) as A
FROM Table1 GROUP BY GroupID, GroupCount
How can I add a where clause that will compare "A" to GroupCount?
You should be able to use a having in your current query.
SELECT GroupID
,GroupCount
,COUNT(*) AS A
FROM Table1
GROUP BY GroupID
,GroupCount
HAVING COUNT(*) = GroupCount

SQL To get Distinct Name and Number from table

Looking for sql to get distinct names and count of those names from a sql table:
Structure:
id
name
other details
Do I use distinct to get each group and then count through those to get:
name1 count(name1)
name2 count(name2)
etc
Thanks
Rob.
When you want a COUNT() or a SUM(), you're using an AGGREGATE FUNCTION based on a GROUP BY clause.
As GROUP BY brings together all records with the same values specified in the GROUP BY columns, you're already getting the same effect as DISTINCT.
Except that DISTINCT doesn't allow aggregates, and GROUP BY does.
SELECT
name,
COUNT(*) AS count_of_name
FROM
yourTable
GROUP BY
name
Try :
SELECT *, COUNT(*) FROM my_table GROUP BY name
Something like this?
select name,COUNT(name) FROM Persons GROUP BY name
In the end I used:
SELECT DISTINCT `school`,COUNT(`school`) AS cat_num FROM table GROUP BY school order by cat_num DESC