Search code examples
sqlsql-serversql-server-2016

SQL Help: Finding lead_time


I have a table for the following structure:

Table name: flight_sessions

session_id    client_id created_at
session_0001    A       2018-02-12 14:00:00
session_0002    B       2018-05-14 08:32:00
session_0002    B       2018-05-14 08:32:00
session_0003    A       2017-02-19 10:30:48
session_0004    K       2018-08-14 14:48:51
session_0004    K       2018-08-14 14:48:51
session_0005    B       2018-07-12 15:17:26
session_0006    K       2018-09-14 06:01:12

So, I have to find the lead_time for each client_id, where lead time is the date difference (disregarding time) between the first and the last created_at date for each client_id.

Any Help would be much appreciated :)

SQL server: 2016


Solution

  • select client_id , datediff(day, min(created_at),max(created_at))
    from tablename
    group by client_id