I am trying to update same table by join of same table, duplicate row skipping.I want to store duplicate value's count in number_of_duplicates field.
UPDATE 4_crem_three_entries a,(
SELECT count(*) c,cream_id FROM 4_crem_three_entries
GROUP BY `c_phone` HAVING count( * ) >1
) b
SET number_of_duplicates=b.c
WHERE a.cream_id=b.cream_id
Try this
UPDATE 4_crem_three_entries
SET number_of_duplicates=(SELECT count( * ) FROM 4_crem_three_entries c where c.cream_id=4_crem_three_entries.cream_id GROUP BY c.c_phone HAVING count( * ) >1)
Related
I have a table like given below name recomendation
I want to delete all the rows where cnt has the minimum value and there exist multiple records of ID_recipient.
If there is a single record of ID_recipient it shouldn't get deleted whatever the cnt value may be.
The ones highlighted in blue are the records that must stay.
I tried:
DELETE from table where(
SELECT DISTINCT(A.ID_recipient), DISTINCT(A.cnt) FROM (
SELECT MIN(cnt) as cnt FROM recomendation_table_ID_recipient GROUP BY ID_recipient HAVING COUNT(*) > 1 ) as A);
which is not working.
If you want to use 2 dimensions you have to use IN clause.
Your subquerys doesn't make much sense, so you should test this first, or post data with wanted example
DELETE from recomendation_table_ID_recipient where (ID_recipient,cnt) IN (
SELECT DISTINCT A.ID_recipient, A.cnt FROM (
SELECT ID_recipient, MIN(cnt) as cnt FROM recomendation_table_ID_recipient GROUP BY ID_recipient HAVING COUNT(*) > 1 ) as A);
delete t1 from recomendation_table_ID_recipient t1 join (
select ID_recipient, min(cnt) as cnt from recomendation_table_ID_recipient
group by ID_recipient
having count(*) > 1
) t2 on t1.ID_recipient = t2.ID_recipient and t1.cnt = t2.cnt;
See db-fiddle
I have selected Duplicate table using Following Query
SELECT
y.Entity,y.ExpenseTypeCode,y.ExpenseType,y.LOB,y.Center,y.Location,y.Amount,y.APSupplierID,y.ExpenseReportID,y.Employee,y.ReportDate,y.ExpenseDate,y.Description
FROM dbo.TotalsByGLCenter$ y
INNER JOIN (SELECT
Entity,ExpenseTypeCode,ExpenseType,LOB,Center,Location,Amount,Description,APSupplierID,ExpenseReportID,Employee,ReportDate,ExpenseDate, COUNT(*) AS CountOf
FROM dbo.TotalsByGLCenter$
GROUP BY Entity,ExpenseTypeCode,ExpenseType,LOB,Center,Location,Amount,APSupplierID,ExpenseReportID,Employee,ReportDate,ExpenseDate,Description
HAVING COUNT(*)>1
) dt ON y.Entity=dt.Entity and y.ExpenseTypeCode=dt.ExpenseTypeCode and y.ExpenseType=dt.ExpenseType and y.LOB=dt.LOB and y.Location=dt.Location and y.Center=dt.Center and y.Amount=dt.Amount and y.APSupplierID=dt.APSupplierID and y.ExpenseReportID=dt.ExpenseReportID and y.Employee=dt.Employee and y.ReportDate=dt.ReportDate and y.ExpenseDate=dt.ExpenseDate and y.Description=dt.Description
i Want Delete selected Query . how can I use delete Query Above Query Statement ?
Try this mate ,
DELETE FROM your_table WHERE id IN (
Select (
your select query
) As alias
)
I have a table full of duplicate data, based on multiple columns. I came up with this query to find all the duplicated rows
select *
from polls
group by server_id, product_id, poll_date
having count(*) > 1;
How do I update these results and set the "updated_by" field to "admin".
I tried doing this, but it doesn't work for me :(
update polls
set updated_by='admin'
group by server_id, product_id, poll_date
having count(*) > 1;
Thanks for your help
You should be able to join your SELECT with the UPDATE.
UPDATE `polls` AS `p1`
INNER JOIN (
SELECT *
FROM `polls`
GROUP BY `server_id`, `product_id`, `poll_date`
HAVING COUNT(*) > 1
) AS `p2`
ON `p2`.`server_id` = `p1`.`server_id`
AND `p2`.`product_id` = `p1`.`product_id`
AND `p2`.`poll_date` = `p1`.`poll_date`
SET `p1`.`updated_by` = 'admin';
Of course it would be better to directly join on the primary key (if you have one).
http://sqlfiddle.com/#!2/d21f3/1
I have a table with some entries here, I want to keep only 50 messages in this table sorted by message_id, and DELETE the rest of entries.
Please help me with the query.
Thanks in advance.
E.g..
DELETE a
FROM chat_history a
LEFT
JOIN
( SELECT x.message_id
FROM chat_history x
JOIN chat_history y
ON y.message_id >= x.message_id
GROUP
BY x.message_id
HAVING COUNT(*) <= 50
) b
ON b.message_id = a.message_id
WHERE b.message_id IS NULL;
http://sqlfiddle.com/#!2/361b4/1
Try this one i have used ORDER BY message_id DESC change it as you want it will delete all except the seleted 50 entries, i have aliased the query because you cannot use same table to select with delete operation
DELETE FROM `chat_history` WHERE id NOT IN ( SELECT t.id FROM
(SELECT id FROM chat_history ORDER BY message_id DESC LIMIT 50 ) t)
I have seen very similar if not same questions on here but my trials of trying to convert following query into an UPDATE statement failed.
SELECT table.* FROM table JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column) WHERE t.rank = 1
ORDER BY t.rank DESC
I want to update column of all results selected using the query above.
How can I convert this into an update statement?
Thank you.
This should do it:
update table
set column = 'somevalue'
where id in
(select id from (
SELECT table.* FROM table JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column) WHERE t.rank = 1) x)
not entirely sure but i think it's something like
update tblname set columname = value where tblname.columncompare = (select statement)
INSERT INTO table (id, value)
SELECT table.id, table.value
FROM table
JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column)
WHERE t.rank = 1
ORDER BY t.rank DESC
ON DUPLICATE KEY UPDATE value = VALUES(value)
Insert on duplicate to the rescue!
Basicly this allows you to do any SELECT as normal and then you prepend INSERT INTO and append ON DUPLICATE.
I guess that this query is made up, but what's the point of filtering and ordering the same column?