query issue with duplicates

Posted on 2015-02-13
Medium Priority
Last Modified: 2015-02-19

I have an sql issue that I am not having any luck finding the solution.

Here is the SQLFiddle with some data.


The issue is when we are displaying featured properties on the website properties are showing up multiple times in the search because they are in the database multiple times with a different property type (wsid in db table)

What I need to happen is

if the property shows up twice in the database (2 properties with the same address) to show the property with wsid=1 and exclude the duplicates

In my sample data properties with the mlsnumber of 115, 114, 108 would be excluded from the search results because the address already exists under the wsid=1

This sort of does what I want:

select * from properties group by address order by address,wsid;

Open in new window

but if I think that is because the data is in the proper order.

I tried changing it to:

select * from properties group by address order by address,wsid desc ;

Open in new window

thinking it would pull in the wsid=3 properties and I did not have any luck..
Question by:rjohnsonjr
LVL 18

Accepted Solution

Simon earned 2000 total points
ID: 40609161
select P.* from properties P inner join
(select address , min(wsid) as minID from properties group by address) sq
on P.address=sq.address AND P.wsid=sq.minID


Expert Comment

by:Smita Melinmani
ID: 40609214
Well I m posting the one that is tested in oracle using analytical function.

select a.wsid,a.address from properties b
(select mlsnumber, wsid,address,row_number() over (partition by address order by wsid) idx
from properties) a
on a.mlsnumber= b.mlsnumber
where a.idx=1

In MySQL I think this needs to be replaced with first_value instead of row_number

I found some link for analytical functions in mysql but I dont have mysql database to test.


Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this article, we’ll look at how to deploy ProxySQL.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question