How to remove duplicate data in a table(MySQL)? [duplicate] - mysql

This question already has answers here:
How to select distinct rows without using group by statement
(4 answers)
Closed 9 hours ago.
I have created a table . In that I have inserted some duplicate data. I tried to delete the duplicate data from the table . But I can't. Please help me in this regard. It will be useful for my project.
I have attached my table content and the code which I tried.
Thanks in advance.
Name
Age
Dept
Vaishu
12
CSE
Vaishu
12
CSE
Ammu
21
ECE
Code which I tried
Errors which I got
EXPECTED OUTPUT
select * from information;
Name
Age
Dept
Vaishu
12
CSE
Ammu
21
ECE

You can use CTE
WITH CTE(Name,
Age,
dept,
DuplicateCount)
AS (SELECT Name,
Age,
dept,
ROW_NUMBER() OVER(PARTITION BY Name,
Age,
dept
ORDER BY name) AS DuplicateCount
FROM tbl)
DELETE FROM CTE
WHERE DuplicateCount > 1;
SELECT * FROM tbl
This will delete identical rows/rows...

Related

How can I select unique results from a table based on a column? [duplicate]

This question already has answers here:
Get top n records for each group of grouped results
(12 answers)
Closed 11 months ago.
I have a 'family' table, with the following columns:
first name
family name
age
I want to query this table such that only ONE member of each family will show up on my result list, and that member must be the oldest, and also limit the result to 25.
Example: imagine the following table with ~500k records.
first_name
last_name
age
john
smith
5
mary
smith
10
jack
son
10
joe
daught
10
The expected result list should return [{mary, smith, 10}, {jack, son, 10}, {joe, daught, 10}].
My current solution is basically to pull the whole table, then remove the 'dupes' manually based on age and last name. While this is "ok", once my dataset gets bigger, it's possibly just wasted processing time.
Is this possible using SQL?
You can use ROW_NUMBER() to assign a numeric value by age (oldest to youngest) withing each family. Then you can pick the first one for each family. For example:
select *
from (
select t.*,
row_number() over(partition by last_name order by age desc) as rn
from t
) x
where rn = 1
When using GROUP BY you will need to use an aggregator (MIN(), MAX(), FIRST n, LAST n, etc.) in the SELECT section:
SELECT MAX(u.age), u.last_name
FROM users AS u
GROUP BY u.last_name
select first_name from table_name group by last_name having max(age)

Is it possible to select a column without aggregating it? [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
How can I SELECT rows with MAX(Column value), PARTITION by another column in MYSQL?
(22 answers)
MySQL - How to select rows with max value of a field
(3 answers)
Closed 1 year ago.
I'm looking for a way to display a column in my table without aggregating it (column store)
SELECT id, name, max(cost), store
FROM test
group by 1,2,4
But in the table im using there are some row that shares the same id,name,license with different machine, and Ideally I'd like to get in return only a single row, with max(cost), if they share the same first 2 columns and only display the last column - without using it to group by.
actual outcome:
id
name
max(cost)
store
1
Joe
30
store1
1
Joe
50
store2
but my desired result will be:
id
name
max(cost)
store
1
Joe
50
store2
Can it even be done?
You seem to want the row with the maximum cost. A typical method uses row_number();
select t.*
from (select t.*,
row_number() over (partition by id, name order by cost desc) as seqnum
from test t
) t
where seqnum = 1;
No aggregation is needed.
You can also use a correlated subquery:
select t.*
from test t
where t.cost = (select max(t2.cost)
from test t2
where t2.id = t.id
);

MySQL: Count occurrences of distinct values for each row

Based on an example already given, I would like to ask my further question.
MySQL: Count occurrences of distinct values
example db
id name
----- ------
1 Mark
2 Mike
3 Paul
4 Mike
5 Mike
6 John
7 Mark
expected result
name count
----- -----
Mark 2
Mike 3
Paul 1
Mike 3
Mike 3
John 1
Mark 2
In my opinion 'GROUP BY' doesn't help.
Thank you very much.
Simplest approach would be using Count() as Window Function over a partition of name; but they are available only in MySQL 8.0.2 and onwards.
However, another approach is possible using a Derived Table. In a sub-select query (Derived Table), we will identify the counts for each unique name. Now, we simply need to join this to the main table, to show counts against each name (while not doing a grouping on them):
SELECT
t1.name,
dt.total_count
FROM your_table AS t1
JOIN
(
SELECT name,
COUNT(*) AS total_count
FROM your_table
GROUP BY name
) AS dt ON dt.name = t1.name
ORDER BY t1.id
If MySQL 8.0.2+ is available, the solution would be less verbose:
SELECT
name,
COUNT(*) OVER (PARTITION BY name) AS total_count
FROM your_table

Count value in SQL [duplicate]

This question already has answers here:
How to count occurrences of a column value efficiently in SQL?
(7 answers)
Closed 5 years ago.
I have got a table with ID's:
Id
===
1
2
2
3
1
2
And I want to create a table with two columns like this:
Id COUNT
=============
1 2
2 3
3 1
How can I do that?
Let's say you called your table 'user', you can try this :
SELECT user.Id as ID, count(user.Id) as COUNT_ID
FROM user
GROUP BY ID;
Hope it helps,
WaLinke
You have to group by your id.
Select id, count(1) as COUNT
from yourtable
group by ID
order by id
That way you tell your sql, that you want to count the number of rows per id.
If you need more examples feel free to google sql count.
There are many good examples out there.
Or check this stackoverflow question:
How to use count and group by at the same select statement
You can use GROUP BY and Count(columnname) functions like this
SELECT
Id,
Count(Id) AS COUNT
FROM
tablename
GROUP BY Id
Something like this should work
SELECT id, COUNT(id) AS Expr1 FROM dbo.Table1 GROUP BY id

Concatenate multiple rows and fields in a table

I want to concatenate fields intersected part of the same table
id user_id user_ip
4 971 108.54.218.114
5 972 108.54.218.114
6 973 108.54.218.114
7 974 108.54.218.114
8 975 107.222.159.246
9 975 98.54.818.133
In the example above, we can see that the user with the IP address (108.54.218.114) address to create multiple accounts with the following account IDs (971, 972, 973, 974), but also that the user with the account ID (975) is connect from the following IP addresses (107.222.159.246, 98.54.818.133)
I want to format the results like this
user_id user_ip
971,972,973,974 108.54.218.114
975 107.222.159.246, 98.54.818.133
MySQL
SELECT
GROUP_CONCAT(DISTINCT users_log.user_id) AS ID_LOG,
GROUP_CONCAT(DISTINCT users_log.user_ip) AS IP_LOG
FROM users_log
GROUP BY users_log.user_id
ORDER BY users_log.user_id DESC
If anyone can help me?
Thank you in advance for your help
There is another recent question (which I can't find) on SO very close to this. I asked about the intersecting row because of the (for lack of better term) recursiveness of this type of solution.
Here is SQL to give you your exact answer for that exact sample set.
SELECT user_id, GROUP_CONCAT(DISTINCT user_ip) AS user_ip
FROM users_log
GROUP BY user_id
HAVING COUNT(*) >1
UNION
SELECT GROUP_CONCAT(DISTINCT user_id) AS user_id, user_ip
FROM users_log
GROUP BY user_ip
HAVING COUNT(*) >1;