count duplicates and non duplicates - sql

Using MS Access SQL
Is it possible to;
list and count all duplicates in one field based on another field?
list all non duplicates in one field based on another field?
Example database below

Based on your results, you just want a simple group by:
select name, year, count(*)
from [table]
group by name, year;
One statement cannot return two different headers. I mean, you could run two queries:
select name, year, count(*) as NumDuplicates
from [table]
group by name, year
having count(*) > 1;
select name, year, count(*) as NumNonDuplicates
from [table]
group by name, year
having count(*) = 1;

Related

SQL/Postgresql: Find duplicates based on all the columns in the table

I have to find duplicates in the table based on all the columns. I know the below query to identify the duplicates based on multiple or single column
select count(*), id, country
from idp.Country_Table
group by id, country
having count(*) > 1
but is there way where we can do it based on all the columns of tables without specifying the columns names? I have 156 columns in table so specifying each column name in the query would be pain.
In Postgres, you can treat the record as an "item":
select ct, count(*)
from idp.Country_Table ct
group by ct
having count(*) > 1;

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;

How to group an already grouped data-set in oracle sql

I want to first group data as per "Roll" and then further group it as per "Name" with different "Marks".
I have grouped data using Group by and having but I am not sure how to further group it.
Group by two columns :
SELECT Roll,Name FROM table GROUP BY Roll,Name;
If you want to see marks also you have aggegrate(sum) it like :
SELECT Roll,Name,sum(Marks)as 'Total Marks' FROM table GROUP BY Roll,Name;
If you want to see marks also you have aggegrate(average) it like :
SELECT Roll,Name,avg(Marks)as 'Average Marks' FROM table GROUP BY Roll,Name;
Refer this image
If I understand correctly, you want roll/name pairs that have multiple distinct values. You can do this using window functions:
select distinct roll, name, marks
from (select t.*, count(distinct marks) over (partition by roll, name) as cnt
from t
) t
where cnt > 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