SQL GROUP BY - also SELECT not-constant columns
- by Michal Kosek
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?