SQL GROUP BY - also SELECT not-constant columns

Posted by Michal Kosek on Stack Overflow See other posts from Stack Overflow or by Michal Kosek
Published on 2012-11-02T16:46:10Z Indexed on 2012/11/02 17:02 UTC
Read the original article Hit count: 323

can someone please help me with this query? I have 2 tables in my database:

log_visitors:
--------------------
id | host_id


log_access:
--------------------
visitor | document | timestamp

"log_access.visitor" links to "log_visitors.id"

Currently, I'm using this query:

SELECT
    log_visitors.host_id
    , MIM(log_access.timestamp) AS min_timestamp
FROM
    log_access
    INNER JOIN log_visitors
        ON (log_access.visitor = log_visitors.id)
GROUP BY log_visitors.host_id;

to get "MIN(timestamp)" for each "host_id" in the database.

HERE'S MY QUESTION:

I also need to get "document" for that access with that timestamp... I can't simply add "log_access.document" into SELECT list, since it's not constant and I am not grouping by document... Any ideas?

© Stack Overflow or respective owner

Related posts about sql

Related posts about select