-2

I would like to count the date-time difference, when I group the data.

Lets have a look on table content example :

id | session_id | created
1  | 101        | 1/10/2010 9:30:10
2  | 102        | 1/10/2010 9:31:10
3  | 101        | 1/10/2010 9:32:10
4  | 103        | 1/10/2010 9:35:10
5  | 102        | 1/10/2010 9:38:10
6  | 103        | 1/10/2010 9:39:10

Output should be as follow :

session_id | time_count
101        | 2 (minutes)
102        | 7 (minutes)
103        | 4 (minutes)

So here i want to calculate time difference of 'created' field for data which is group by session_id.

Any help would be great. Thanks in advance :)

My Situation :

I have a table with following fields :

id, session_id, platform, event, created

there are 2 event (Start,End)

Now if there are records of Start and End for same session_id then i want the time takes to complete this session.

But if i have 2 Start session, but only 1 End session then i do not want the time difference for 2nd Session because it did not end

4
  • I agree that you need to describe the situation more. However please add the DDL statements with INSERT statements as well. It will help to give you the right answer May 3, 2018 at 5:56
  • What version of SQL Server are you using? May 3, 2018 at 6:14
  • I am using sql server 5.6
    – KMG
    May 3, 2018 at 6:16
  • Try my Updated answer. Hope this helps.
    – DineshDB
    May 3, 2018 at 10:08

3 Answers 3

0
SELECT session_id,
DATEDIFF(MINUTE, MAX(created), MIN(created)) AS diff
FROM table
GROUP BY session_id
0

Try this:

Table Schema:

CREATE TABLE A(id INT, session_id INT,Event VARCHAR(20), created DATETIME);

INSERT INTO A VALUES(1, 101,'Start','2010/10/01 9:30:10')
,(2, 102,'Start'  , '2010/10/01 9:31:10')
,(3, 101,'End'    , '2010/10/01 9:32:10')
,(4, 103,'Start'  , '2010/10/01 9:35:10')
,(5, 102,'End'    , '2010/10/01 9:38:10')
,(6, 103,'End'    , '2010/10/01 9:39:10')
,(7, 101,'Start'  , '2010/10/01 9:39:10')
,(8, 102,'Start'  , '2010/10/01 9:39:10')
,(9, 101,'End'    , '2010/10/01 9:55:10');

Query:

SELECT D.session_id
    ,TIMESTAMPDIFF(MINUTE,MIN(D.created), MAX(D.created)) time_count
FROM(
  SELECT a.id,a.session_id,a.created
    ,ROUND(count(*)/2,0) as RN 
    ,count(*) as row_number1 
  FROM a AS a
  JOIN a AS b ON a.session_id = b.session_id AND a.id >= b.id
  GROUP BY a.id,a.session_id,a.created
  ORDER BY 2,3
    )D
GROUP BY D.session_id,D.RN
HAVING COUNT(1)>1

Output:

session_id  time_count
101         2
102         7
103         4
101         16

Fiddle:

Check the Output in the #SQL Fiddle

-1

You can try it's sintax :

    WITH cte AS (
    SELECT
        session_id,
        DATEDIFF(minute, created, LAG(created, 1)) AS diff,
        ROW_NUMBER() OVER (PARTITION BY session_id ORDER BY created) rn
    FROM yourTable
)

SELECT

        session_id,
        diff AS time_count
    FROM cte
    WHERE rn % 2 = 0

;
1
  • 1
    Would you like to improve this code-only answer by adding an explanation? How does this work to solve OPs problem? You might also want to highlight the differences to other, similar, older answers.
    – Yunnosch
    May 3, 2018 at 6:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.