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

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;

Related

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;

SQL : Find Numbers of Rows in a Table according to Criteria

I want to get numbers of rows in a table according to certain criteria.
Please see the below table:-
Herein I want to get numbers of rows according to Column StationTo.
I want to get numbers of rows of each StationTo entries.
You could group by the StationTo and use the aggregate count(*) function:
SELECT StationTo, COUNT(*)
FROM mytable
GROUP BY StationTo
EDIT:
If you just want the number of rows for a single StationTo, you could use a where clause:
SELECT COUNT(*)
FROM mytable
WHERE StationTo = 'P11004400000'
Hi have you master table for stationTo records?
select s.stationto, count(data.*) from stationtomaster
left join data on data.stationto=stationtomaster.stationto
group by s.stationto
Select StationTo,Date, count(*) from table group by StationTo, Datemeaning all the stationTo having rows display their count.
or select count(distinct StationTo) from table or Select count(*) from table where stationTo='yourvalue'

Oracle SQL to get Unique Records

Does anyone know the sql to pull 4 rows from the following table which contains 8 rows?
Just want one row for each arbitrary person.
The real data will be thousands of records so it must be generic and use only the ID's not the names.
table
You seem to have a symmetric relationship. So, you can do:
select t.*
from t
where t.id < t.pid;
select
ID,
FName,
LName
from your_table
union
select
PID,
PFName,
PLName
from your_table
order by 3, 2, 1

count duplicates and non duplicates

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;

How can I select an entire row without selecting rows where a certain column value is duplicated?

How can I select an entire row without selecting rows where a certain column value is duplicated?
EDIT: Sorry my question is a bit vague. Assuming i have a table with two columns: names and score. There are duplicate values in names. I want to select distinct names and also select their scores.
Based on the edited information given, this will use the GROUP_CONCAT function to produce the distinct names and a comma-delimited list of scores. If more appropriate, you could substitute another aggregate function (e.g., MIN, MAX, SUM) for the GROUP_CONCAT.
SELECT Name, GROUP_CONCAT(Score)
FROM YourTable
GROUP BY Name
Using the following example data...
name score
----- -----
James 10
James 12
Lisa 45
John 42
...the following queries should return the third and fourth row.
select name, score
from table
where name in(select name
from table
group by name having count(*) = 1);
...less clear, but probable more efficient on MySQL.
select t1.name, t1.score
from (select name
from table
group by name having count(*) = 1
) t1
join table t2 on(t1.name = t2.name)
Try using the DISTINCT clause on the columns you don't want duplicates of.
based on your latest edit, try this:
select
name, SUM(score) AS TotalScore
from YourTable
group by name