2

Pardon the poor title of this question, it's difficult to name and I'll gladly change it if someone knows a more appropriate title.

I have a table with 3 fields:

• user_id
• session_id
• session_time

Since a user can visit the website several times, what I would like is the session_id of the first ever visit of an individual. This will subsequently be used in a sub query.

I am able to get what I want with the following query, but I thought there's probably a neater, shorter and more elegant way. Is there?

select
    session_id
from
    contact_web_sessions parent
inner join

    (
    select
        user_id,
        min(session_time) as session_time
    from
        contact_web_sessions
    group by user_id
    ) sub

    on sub.user_id = parent.user_id and sub.session_time = parent.session_time
Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • 1
    Your query is the most common way to do it. See the linked question for other techniques. – Barmar Dec 29 '15 at 17:32

0 Answers0