SELECT COUNT(DISTINCT User_Id) FROM (
SELECT User_Id
FROM t1
GROUP BY User_Id, Action_Type
HAVING COUNT(*) > 1
) t
SQL count of return users
Question
I'm trying to return a count of return users which happens when there is a duplicate 'user_id and action_type'.
So if you refer below, I would like my output to be = 2, since user_id (5) has 2 similar action_types (234) and user_id (6) also has 2 similar action_types (585).
How do I structure my query to reflect this?
Table t1
User_Id Action_Type
--------- ------------
5 234
5 846
5 234
6 585
6 585
7 465
La solution
Autres conseils
SELECT COUNT(User_ID) DuplicateRecordsUsers
FROM
(SELECT User_ID, Action_Type, COUNT(User_ID) Records
FROM Table
GROUP BY User_ID, Action_Type
HAVING COUNT(User_ID) > 1
)
SELECT COUNT(User_Id) FROM (
SELECT User_Id
FROM t1
GROUP BY User_Id, Action_Type
HAVING COUNT(*) > 1
) t
DISTINCT
not required just count the ids returned
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow