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;
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 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 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 ;
I have two similar tables that I need to locate any records that exist in one table but not the other, but the only value I have to find these records have multiple duplicate values.
Table 1:
TId Date TYPE AMOUNT
1 2014-02-01 23 25.34
2 2014-02-01 23 46.95
3 2014-02-01 23 46.95
4 2014-02-01 23 25.34
5 2014-02-01 23 21.01
Table 2:
TId Date TYPE AMOUNT
7 2014-02-01 23 25.34
8 2014-02-01 23 46.95
9 2014-02-01 23 21.01
I need to query the two tables to locate records 3 and 4 from table 1. The issue is I'm finding a match in records 7 and 8 in table 2. I've used 'NOT EXISTS' and 'JOIN' but can't seem to get the results I'm looking for.
I am not 100% sure on what you want to find, but it sounds like you want the records on table 1 that are not on table 2, based on the date, type and amount.
If so a left join and checking for NULL on a field is probably the best way to do it:-
SELECT table1.*
FROM table1
LEFT OUTER JOIN table2
ON table1.Date = table2.Date
AND table1.Type = table2.Type
AND table1.Amount = table2.Amount
WHERE table2.TId IS NULL
Possibly like this (depending on your exact requirements). This should get all the records from table1, and join that against a record where the date, type and amount match AND the tid is the first for that record. Then joined against table2. Records put out if there is no match on table2, or there is a match but the records on table1 is not the first one that would match.
The does only work if there are no duplicates on table2.
SELECT table1.*
FROM table1
LEFT OUTER JOIN
(
SELECT Date, TYPE, AMOUNT, MIN(TId) AS TId
FROM table1
) t11
ON table1.Date = t11.Date
AND table1.Type = t11.Type
AND table1.Amount = t11.Amount
AND table1.TId = t11.TId
LEFT OUTER JOIN table2
ON table1.Date = table2.Date
AND table1.Type = table2.Type
AND table1.Amount = table2.Amount
WHERE (t11.TId IS NULL
AND table2.TId IS NOT NULL)
OR table2.TId IS NULL
For the following table, is it possible to get the result using a self-join?
Table:
id pId type
-------------
1 1000 1
2 1001 1
3 1002 1
4 1000 3
Expected result:
id pId type
-------------
2 1001 1
3 1002 1
In other words, I want all the rows which has type 1, but does not have type 3.
Thank you in advance.
UPDATE
this is a question in the context of a performance testing.
in other words, there are many rows like 1000 and 1001, 1002.
i'm trying to improve the performance using the current table structure.
but i understand that probably the table is not well-designed as well.
You don't need any joins - just a subselect - something like this:
select * from mytable t1
where not exists (select id from mytable t2 where t1.pid=t2.pid and type=3)
If you want to use a self join, you could use this query:
SELECT t1.id, t1.pId, t1.type
FROM
tablename t1 LEFT JOIN tablename t2
ON t1.pid = t2.pid AND t2.type=3
WHERE
t1.type=1 AND
t2.type IS NULL
Please see fiddle here.