Navin Bhasker
Navin Bhasker

Reputation: 11

Query that selects userId and average session duration for each user who has more than one session

App usage data are kept in the following table:

Table sessions:

id INTEGER PRIMARY KEY,
userId INTEGER NOT NULL,
duration DECIMAL NOT NULL

I need a query that selects userId and average session duration for each user who has more than one session.

Upvotes: 0

Views: 4874

Answers (2)

flyingfox
flyingfox

Reputation: 13506

SELECT userId,AVG(duration),count(id) as num 
     FROM sessions GROUP BY userId HAVING count(id) > 0

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522292

I would write this as:

SELECT userId, AVG(duration)
FROM sessions
GROUP BY userId
HAVING COUNT(*) > 1;

Upvotes: 6

Related Questions