+ 3
SQL Query HELP | Advice needed !
Hello everyone! I am having trouble writing a query for the following Problem : App usage data are kept in the following table: TABLE sessions id INTEGER PRIMARY KEY, userId INTEGER 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. I am not sure I understand how to write the query that selects a userID that appears more than one times in the table. Any help would be great!
5 Respostas
+ 6
Well, for sure I found a solution that works:
SELECT userId, AVG(duration)from sessions
GROUP BY userId
HAVING COUNT(userId)>1 ;
I am quoting from w3schools :
The HAVING clause was added to SQL because the WHERE keyword
could not be used with aggregate functions.
Thank you so much Michelle and Ankit for reaching out !
+ 7
SELECT DISTINCT userId, AVG(duration) from sessions WHERE COUNT(userId)>1;
+ 3
i'm not sure but try this if it works:
SELECT DISTINCT userID, AVG(duration)
FROM sessions
WHERE (SELECT COUNT(userId) AS countUsers
FROM sessions
WHERE countUsers >1)
let me know if it works... i can't test it now
0
SELECT userId, AVG(duration)from sessions
GROUP BY userId
HAVING COUNT(userId)>1 ;
0
Answer is Select * From users Limit 10.