Complex Joins in Postgresql -


it's possible i'm stupid, i've been querying , checking hours , can't seem find answer this, apologize in advance if post redundant... can't seem find doppelganger.

ok: have postgresql db following tables:

key(containing 2 fields in i'm interested, id , name) , second table, key.

data contains well... data, sorted id. id unique, each name has multiple id's. e.g. if bill enters building id 1 bill. mary enters building, id 2 mary, bill re-enters building, id 3 bill.

the id field in both key table, , data table.

what want is... find

the max (e.g. last) id, unique each name, , data associated it.

e.g. bill - last login: id 10. time: 123utc door: west , on.

so... i'm trying following query:

select  *    data, key    key.id = (      select        max (id)            key     group id ) 

here's kicker, there's about... 800m items in these tables, errors are... time consuming. can see if query gonna expect?

thanks much.

to maximum key each name . . .

select name, max(id) max_id data group name; 

join other table.

select * key t1 inner join (select name, max(id) max_id             data             group name) t2 on t1.id = t2.max_id 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -