0

I am having trouble getting this query correct. I am using Postgres and trying to query the last 15 clients viewed by a user.

Table client
Columns id, name...

Table impressions
Columns imressionable_id, user_id, created_at ...

The impressionable_id is equal to the client_id and the tables can be joined on this field.

I need to get a list of the last 15 DISTINCT client names viewed by the user and ordered DESC by the created_at date. The problem is currently a query of the impressions table will produce many duplicate entries but with unique created_at timestamp as each time you view a client it creates a new impressions record.

So, a query of

select impressions.impressionable_id, created_at from impressions where user_id = 1 order by created_at DESC  

will produce:

  impressionable_id   created_at
        1            2014-11-14 21:44:47.705167 
        1            2014-11-14 21:32:15.411488 
        3            2014-11-14 18:43:26.020719 
        1            2014-11-14 18:42:15.974442
        5            2014-11-14 18:41:10.609617 
        3            2014-10-29 20:53:01.383896

I need a query that will give me:

1    John Doe     2014-11-14 21:44:47.705167 
3    Jay Smith    2014-11-14 18:43:26.020719
5    Tim Jones    2014-11-14 18:41:10.609617 

and so on. Any help is greatly appreciated!

1
  • select max(created_at) .... group by impressionable_id? Commented Feb 23, 2015 at 21:04

2 Answers 2

1

Use MAX() and GROUP BY:

select I.impressionable_id,c.name, MAX(I.created_at) as created_at 
from impressions JOIN
     client c ON c.id = I.user_id
group by impressionable_id,c.name
order by created_at DESC
limit 15;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but this gets the records from just the one table.
@RKRaider: That is because, I didn't know your table structure. Now I have updated my answer with a join.
This is my working solution with the slight exception of the join clause being on I.impressionable_id not user_id. I then added a where clause for the user_id.
1
SELECT y.* FROM 
(
    SELECT 
        DISTINCT ON (x.id) x.id, x.name, x.created_at 
    FROM (SELECT c.id, c.name, i.created_at FROM clients c RIGHT JOIN impressions i ON c.id = i.user_id) x
) y 
ORDER BY y.created_at DESC LIMIT 15;

3 Comments

I gave this the answer as I was able to fix it adding the fix: c.id = i.impressionable_id where i.user_id =1 in lieu of c.id = i.user_id.
After careful testing this works on the first record only then it does not get the latest records for the next client ids.
@RKRaider Check the screenshot: http://i.gyazo.com/bf4e470fbac69e330810ca5e5f7c9945.png I created both your tables and run the query. Are you missing something saying that it doesn't work properly ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.