I have a table in my MySQL database that I need to remove the duplicates from.
My table looks something like this:
unique_id value frequency value_type publication_date
1 6.5 1 2 2014-12-31
2 7.5 3 5 2014-06-04
3 6.5 1 2 2015-07-13
4 8.0 4 3 2010-12-31
Rows 1 and 3 are duplicates except for the publication_date. I need to remove these duplicates but keep the row with the max publication date so for this example I would want to remove row 1 and keep row 3.
So far I've tried this but it's giving me too many results on my test table:
SELECT t.* FROM
(SELECT MAX(publication_date) AS most_recent_date
FROM table_1
GROUP BY `value`,frequency,value_type
) t1
JOIN table_1 t
ON t.publication_date = t1.most_recent_date;
Any help would be appreciated.
Thanks.
You can use JOIN to remove the duplicates as
delete t1 from table_1 t1
join table_1 t2 on t1.value = t2.value
and t1.frequency= t2.frequency
and t1.value_type = t2.value_type
and t1.unique_id <> t2.unique_id
and t1.publication_date < t2.publication_date ;
Related
My tables look like this. my op and country is having many to many relationships with each other.
OP
id, name,.....
op_country
id, op_id, country_id
country
id, name, ...
my op_country filled like below
id op_id country_id
1 1 1
2 1 2
3 2 2
4 2 3
5 3 3
6 3 3
7 1 1
I want to remove my duplicate entries from op_country. Here I want to remove rows 6 and 7 since we already have rows with such values.
How can I do that.
DELETE t1
FROM op_country t1
JOIN op_country t2 USING (op_id, country_id)
WHERE t1.id > t2.id
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=247ebc5870a6ab10b64076ffb375797f
You want to delete entries for which exists a sibling with a lower ID:
delete from op_country
where exists
(
select null
from (select * from op_country) op2
where op2.op_id = op_country.op_id
and op2.country_id = op_country.country_id
and op2.id < op_country.id
);
The from (select * from op_country) is necessary instead of a mere from op_country due to some weird restriction in MySQL updates.
I have a very strange problem that I am not able to solve at the moment.
I have two tables. One table contains the IDs and the other table contains transactions.
Let's say IDs are like:
1
2
2
3
4
5
6
6
and they are joined using the IDs. So what I want to do is if a transaction is there is the transaction table it should be recorded once for each id.
For Example if there is a transaction for ID = 6:
6 23
6 23
23 is the transaction amount.
It should be shown as:
6 23
6 NULL
If I understand correctly, you just want the value once even when the ids are duplicated. If so, you can use row_number():
select t1.id,
(case when row_number() over (partition by t1.id order by t1.id) = 1
then t.amount
end)
from table1 t1 left join
transactions t
on t1.id = t.id;
I need to get all the records where table1.user is in table2.userid and table2.country = 8 and table2.status = 1
This is the sample data in my database
table1
id user
-- ----
1 12
2 23
3 34
4 32
5 85
6 38
table2
id userid country_id status
-- ---- ----- ----
1 12 5 1
2 12 8 1
3 85 8 1
4 38 8 0
5 38 7 1
6 23 8 1
7 23 4 1
in this case I should only get the id #3 in table2
Inner join will do the job taking only record with match in the two tables.
SELECT *
FROM table1 t1
INNER JOIN table2 t2 on t1.user = t2.userid
WHERE t2.country=8 and t2.status=1
You mean
"get all the records where table1.user is in table2.userid
with table2.country = 8 and table2.status = 1 ?"
If so, then:
Select * from table1 t1
Where Exists(Select * from table2
Where userId = t1.user
and country = 8
and status = 1)
Well to accomplish that is quite simple, you just need to make use of an inner join. An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
In your case, the INNER JOIN will return all rows from table2 where the join condition is met.
For a more detailed explanation and examples just visit this link: http://www.w3schools.com/sql/sql_join.asp
based on your response to Adam's approach "
I tried this but it didn't work, yes it gets the records but the id #2 in table2 is also included. what I need to get is the id #3 in table2. Thanks :) ". I changed my SQL query to:
SELECT
*
FROM
table1
INNER JOIN
table2
ON
table1.user = table2.userid
WHERE
table2.userid = 85
I assumed you only want the data of that particular userid in table2
I have 2 tables in Mysql. I need to join them somehow to get one value from second table into the first one.
TABLE 1
Day EmployeeId Total EmployeeName
1 2 20 Josh
1 1 20 Mike
2 2 5 Josh
2 1 10 Mike
3 3 5 Eric
TABLE 2
Day EmployeeId Max_Total
1 2 40
1 1 40
2 2 5
2 1 15
I need to get something like TABLE 3
Day EmployeeId Total EmployeeName Max_Total
1 2 20 Josh 40
1 1 20 Mike 40
2 2 5 Josh 5
2 1 10 Mike 15
3 3 5 Eric null
So this Max_Total column needs to be somehow created and populated.
This Day_EmployedId combination is unique in both tables and that should be used somehow to extract values from 2nd table and add it to the first one.
Sometimes first table can have more values, sometimes the second one, but the first one will always be the one that needs to be manipulated/added to.
Any hint will be appreciated. Thanks
You are looking for a left join on two fields:
select t1.*, t2.max_total
from table1 t1 left join
table2 t2
on t1.day = t2.day and t1.employeeid = t2.employeeid;
I would not recommend actually updating table1. You can generate the data as you need it. However, in order for an update to work, you need to add a column to the table first, and then update it.
You need to separate your tasks.
Alter your Table1 to add the column Max_Total
Write UPDATE query to update your Max_Total in your Table1.
Query:
UPDATE t1.Max_Total = t2.Max_Total
SET t1.
FROM Table1 t1
JOIN Table2 t2 ON t1.Day = t2.Day AND t1.EmployeeId = t2.EmployeeId
If you are only concerned about getting a combined result set
SELECT t1.Day, t1.EmployeeId, t1.Total, t1.EmployeeName, t2.Max_Total
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Day = t2.Day AND t1.EmployeeId = t2.EmployeeId
For more information on LEFT JOIN, you can study this tutorial.
I have a table:
Table1:
Id value
1 10
2 20
3 30
4 40
In table 2:
Id
1
1
2
2
3
4
4
4
I want to get the results:
Table2:
Id value
1 10
1 10
2 20
2 20
3 30
4 40
4 40
4 40
I know add a column with following code:
ALTER table Table2 ADD value int(11)
How to fill the data??
I think these SQL useful to you.
UPDATE Table2 LEFT JOIN Table1 ON Table2.id = Table1.id
SET Table2.value = Table1.value
Thank you.
you need to first ALTER the table and then fill the values using JOIN command as:
ALTER table Table2 ADD value int(11);
Then insert / fill the values as:
UPDATE Table2 INNER JOIN Table1
ON Table2.id = Table1.id
SET Table2.value = Table1.value
You could run this update command juste after tours.
UPDATE Table2 SET value = (SELECT value FROM Table1 WHERE Table2.Id = Table1.Id)