Link to home
Start Free TrialLog in
Avatar of Randy Johnson
Randy JohnsonFlag for United States of America

asked on

query issue with duplicates

Hello,

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

Here is the SQLFiddle with some data.

http://sqlfiddle.com/#!2/445f10/1


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..
ASKER CERTIFIED SOLUTION
Avatar of Simon
Simon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well I m posting the one that is tested in oracle using analytical function.

select a.wsid,a.address from properties b
join
(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.

http://explainextended.com/2009/03/10/analytic-functions-first_value-last_value-lead-lag/