Solved

query issue with duplicates

Posted on 2015-02-13
2
85 Views
Last Modified: 2015-02-19
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..
0
Comment
Question by:rjohnsonjr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Accepted Solution

by:
Simon earned 500 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

http://sqlfiddle.com/#!2/445f10/16
0
 
LVL 2

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
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/
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Generate PDF from MySQL using PHP 3 54
add criteria to query in VB, Access 2003 2 32
MySQL-Design Help 12 44
Need to create and populate a column map table 5 21
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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