Similar to this Find duplicate records in MySQL, I want to pull out duplicate records in a MySQL database based on the value of a specific column.
To pull out the one with the highest ID, I can do:
SELECT MAX(id), name, address, no_purchase, count(id) as cnt FROM list
GROUP BY name, address HAVING cnt > 1
To pull out the one with the lowest ID, I can do:
SELECT MIN(id), name, address, no_purchase, count(id) as cnt FROM list
GROUP BY name, address HAVING cnt > 1
How can I pull it based on the highest no_purchase? When I do MAX(no_purchase), I didn't get the correct id.
One generic solution is this:
SELECT *
FROM t
WHERE (name, address, no_purchase) IN (
SELECT name, address, MAX(no_purchase)
FROM t
GROUP BY name, address
)
Having said that, using name and address to identify duplicate people does not always work.
Related
I want to fetch the name of person having 3rd highest salary. Is there any way to display any custom message for departments having less than 2 employees?
Eg: In lag() we can use something like LAG(salary, 1, 'first in list').
Currently it shows null
table data
Since I refuse to open your link, I don't know if the table name and column names in my query are exactly as you need. If not, just change them.
SELECT department, name
FROM
(SELECT department, name, salary, ROW_NUMBER() OVER
(PARTITION BY department ORDER BY salary DESC) AS rn
FROM yourtable
) sub
WHERE rn = 3
UNION ALL
SELECT department, 'No One'
FROM yourtable
GROUP BY department
HAVING COUNT(department) < 3
ORDER BY department;
There might be shorter options, but this one is quite clear: The first query finds the department and the name of people having the 3rd highest salary in this department. It doesn't select anything for departments with less than three people.
The second query will find all departments having less than three people.
Sidenote: This query will only take one person if multiple persons in a certain department have the same 3rd highest salary.
Due to your less detailed description, it's unclear if this is correct for you. It's up to you to adjust this if necessary.
I have a table with the following values: Name, Street,I'd , Value, Date.
I need to combine Name, Street, Id and make 2 subgroups by date. I want to compare the value in row with the same name, street and id but different date. And write only the ones with different value
Example:
Mike, Street 1 , idtag , 5 , 11.5.2022
Mike, street 1 , idtag , 10 , 10.5.2022
I want to write the difference in value with the name, street, id combination.
All the solutions I have tried take way to long
dYou could use an aggregation approach here. Assuming that you want to flag any name, street, and ID combination which have 2 or more records on different dates, you may try:
SELECT name, street, ID, MAX(val) - MIN(val) AS diff
FROM yourTable
GROUP BY name, street, ID
HAVING MIN(date) <> MAX(date);
To use this logic for a specific pair of records, whose (unique) date values are known, use this version:
SELECT name, street, ID, MAX(val) - MIN(val) AS diff
FROM yourTable
GROUP BY name, street, ID
HAVING MIN(date) = '2022-05-10' AND MAX(date) = '2022-05-11';
The following query works great to count and get the total sum of visitors to my page.
SELECT id, COUNT(id) AS count
FROM visits
WHERE status <> 'test'
GROUP BY id
UNION ALL
SELECT 'SUM' id, COUNT(id)
FROM visits
WHERE status <> 'test'
But inside the visits table I have a column called IP where I store the IP of the visitors. I want to change the above query so that visits with the same IP are not counted (I only want to count unique IPs). How can I do it?
I have tried HAVING COUNT(IP) > 1 but it doesn't work.
Thanks!!!
Funny one this,
I've got a table, "addresses", with a list of address details, some with missing fields.
I want to identify these rows, and replace them with the previous address row, however these must only be accounts that are NOT the most recent address on the account, they must be previous addresses.
Each address has a sequence number (1,2,3,4 etc), so i cab easily identify the MAX address and make that it's not the most recent address on the account, however how do I then scan for what is effectively, "Max -1", or "one less than max"?
Any help would be hugely appreciated.
Try this:
SELECT MAX(field) FROM table WHERE field < (SELECT MAX(field) FROM table)
By the way: Here is a good article, which describes how to achieve nth row.
SELECT TOP 1 field
FROM(
SELECT DISTINCT TOP 2 field
FROM table
ORDER BY field DESC
)tbl ORDER BY field;
This returns 1st or nth max record.
;WITH Distincts as (
SELECT DISTINCT field from table
)
,
NextMax as (
select field, ROW_NUMBER() over (order by field desc) as RN from Distincts
)
select * from NextMax where RN = 2
I have a voting application that writes values to a mysql db table. It is a preference/weighted voting system so people choose a first option, second option, and third option. These all go into separate fields in the table. I'm looking for a way to write a query that will assign numerical values to the responses (3 for a first response, 2 for a second, 1 for a first) and then display the value with the summed score. I've been able to do this for total number of votes
select count(name) as votes,name
from (select 1st_option as name from votes
union all
select 2nd_option from votes
union all
select 3rd_option from votes) as tbl
group by name
having count(name) > 0
order by 1 desc;
but haven't quite figured out how to assign values to response in each column and then pull them together. Any help is much appreciated. Thanks!
You could do something like this:
select sum(score) as votes,name
from (select 1st_option as name, 3 as score from votes
union all
select 2nd_option as name, 2 as score from votes
union all
select 3rd_option as name, 1 as score from votes) as tbl
group by name;