Answers for "verage session duration for each user who has more than one session"

0

verage session duration for each user who has more than one session

/*App usage data are kept in the following table:

TABLE sessions
  id int PRIMARY KEY,
  userId int NOT NULL,
  duration DECIMAL NOT NULL
Write a query that selects userId and average session 
--duration for each user who has more than one session.

*/
-- Suggested testing environment: 
-- http://sqlite.online/

-- Example case create statement:
DROP TABLE IF EXISTS sessions;
CREATE TABLE sessions (
  id int NOT NULL PRIMARY KEY,
  userId int NOT NULL,
  duration DECIMAL NOT NULL
);

INSERT INTO sessions(id, userId, duration) VALUES(1, 1, 10);
INSERT INTO sessions(id, userId, duration) VALUES(2, 2, 18);
INSERT INTO sessions(id, userId, duration) VALUES(3, 1, 14);
-- the resultant query write only this into testdome
Select userId, CAST(Avg (duration) as int)
from sessions 
group by userId having count(userId) > 1;
-- Expected output:
-- UserId  AverageDuration
-- -----------------------
-- 1       12
Posted by: Guest on March-24-2021

Browse Popular Code Answers by Language