Jump to content

Need help writing a query


sptrsn

Recommended Posts

I have two tables, that between them contain a complete record on property foreclosure data. (apn, adddress, sale date, open bid etc).

 

apn is unique in T1 and the data in T1 is the data that does NOT change. (ie apn and address)

 

In T2, we are storing the data that changes as time progresses. (ie sale date and open bid)

 

Each time it changes, we insert this newest data into T2, along with a date/time stamp and of course the related apn.

 

So, in T1, there is a unique record for each 'apn' with static data related to that property. (stands for assessors parcel number)

However, T2, there are several records with the same 'apn', differing values for sale_date and or open_bid, and a date/time when it was inserted.

 

I need a query that will join on T1.apn=T2.apn. However, I need the most recent matching T2.apn based on the 'update_date'.

 

I know this is not even close, but I have no idea how to get there from here.

 

select * from T1 join T2 on T1.apn=T2.apn 

 

Sure would appreciate any help you feel inclined to share.

Thanks

Link to comment
Share on other sites

that didn't quite work.

 

the goal is to have one record for each apn joined with only the most recently updated matching apn in T2.

 

I was told that it would somehow involve sorting T2 then limit 1.

 

But if I add limit 1 to the end of that statement, I get one record total. I need one row for each unique apn.

 

Thanks.

Link to comment
Share on other sites

yea, that was what I tried, but it returns one single record.

 

I think that there is somehow a query within the query. Where in the inner query, we query T2 and sort and limit 1 per apn, then the it's joined with the matching apn from T1.

 

My coder had written this for something else and I got the impression that it would somehow be similar.

 

select * from `T1` where apn in (
select apn from `T2` 
group by apn having count(*) > 1)
order by apn

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.