mysql select count for each user if greater than 3

mOna

I have the following mysql table (the real table is actually very huge, about 2 million rows):

----------------------------
userId | artistId | trackId
----------------------------
user1  | artist1  | track1
-------|----------|--------
user1  | artist1  | track1
-------|----------|--------
user1  | artist1  | track1
-------|----------|--------
user2  | artist1  | track1
-------|----------|--------
user2  | artist2  | track2
-------|----------|--------
user2  | artist2  | track2
-------|----------|--------
 ....  |   ....   |  ....

What I would like to is: for each user, select artists that users listened more than 3 different tracks of them (i.e., 3 tracks of the same artist). This is because I need to consider this selection as users preferences on artists, so if e.g, a user listened only to 1 or two tracks of an artist, I don't want to consider it as "preferences/likes". Here is the query that I wrote but I am not sure if this is correct:

select p.userId, p.artistId, p.trackId 
from lastfm_part2 p 
join 
(select userId, artistId, trackId 
from lastfm_part2 
group by userId, artistId, trackId 
having count(trackId) > 3) as m 
on m.userId = p.userId and m.artistId = p.artistId and p.trackID = m.trackId

PS. I need to return all the rows, even though they may seem to be duplicates (same user, same track, same artist), but in reality they are related to different time stamps. I appreciate if someone help me understand if this query is correct.

Thanks

massie

I tested it in Oracle so maybe MySQL is a little different but the next query did work for me.

SELECT p.userId, p.artistId, COUNT( DISTINCT p.trackId )
FROM lastfm_part2
group by userId, artistId
having count( DISTINCT p.trackId ) > 3;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql select if count of values for each user greater than 1

From Dev

MYSQL Select Count if greater than 0

From Dev

Select MySQL results, ignoring fields that have a count greater than X

From Dev

MySQL count rows where column value is same and select them where count is greater than 2

From Dev

SQL select if count on relation is greater than X?

From Dev

Update if count is greater than 5 in MySQL

From Dev

MySQL Select Last Entry for each User, than the next entry

From Dev

MySQL select rows with two where conditions and count greater than 1 with same column ID

From Dev

MySQL select rows with two where conditions and count greater than 1 with same column ID

From Dev

select count of each user privileges and pages from 4 tables mysql

From Dev

MYSQL Select Records Greater Than OR First

From Dev

Mysql Select datetime, greater than timestamp

From Dev

MySQL select records with sum greater than threshold

From Dev

count the number of values greater than each value in a column of an array in r

From Dev

count of comments by each user - mysql

From Dev

SQL select from inner join where count greater than

From Dev

Select Where Count() of multiple columns is greater than one

From Dev

Select Count Where Values greater than 0 SQL Server

From Dev

SQLite select all records for which count is greater than 1

From Dev

SQL select from inner join where count greater than

From Dev

SQL query - select max where a count greater than value

From Dev

MySQL query to count results greater than 10 then group by

From Java

InfiniteScroll + isotope : loadNextPage until item count greater than 3

From Dev

Output all rows with word count in a column greater than 3

From Dev

WordPress - Display categories only if post count greater than / equal to 3

From Dev

Excel: count numbers greater than 3 in strings in range of cells

From Dev

How to user a 'greater than' PHP statement with MySQL Decimals

From Dev

MySQL: Select TIMEDIFF values where the difference is greater than x

From Dev

MySQL: Select rows with values nearest but greater than a given value

Related Related

  1. 1

    mysql select if count of values for each user greater than 1

  2. 2

    MYSQL Select Count if greater than 0

  3. 3

    Select MySQL results, ignoring fields that have a count greater than X

  4. 4

    MySQL count rows where column value is same and select them where count is greater than 2

  5. 5

    SQL select if count on relation is greater than X?

  6. 6

    Update if count is greater than 5 in MySQL

  7. 7

    MySQL Select Last Entry for each User, than the next entry

  8. 8

    MySQL select rows with two where conditions and count greater than 1 with same column ID

  9. 9

    MySQL select rows with two where conditions and count greater than 1 with same column ID

  10. 10

    select count of each user privileges and pages from 4 tables mysql

  11. 11

    MYSQL Select Records Greater Than OR First

  12. 12

    Mysql Select datetime, greater than timestamp

  13. 13

    MySQL select records with sum greater than threshold

  14. 14

    count the number of values greater than each value in a column of an array in r

  15. 15

    count of comments by each user - mysql

  16. 16

    SQL select from inner join where count greater than

  17. 17

    Select Where Count() of multiple columns is greater than one

  18. 18

    Select Count Where Values greater than 0 SQL Server

  19. 19

    SQLite select all records for which count is greater than 1

  20. 20

    SQL select from inner join where count greater than

  21. 21

    SQL query - select max where a count greater than value

  22. 22

    MySQL query to count results greater than 10 then group by

  23. 23

    InfiniteScroll + isotope : loadNextPage until item count greater than 3

  24. 24

    Output all rows with word count in a column greater than 3

  25. 25

    WordPress - Display categories only if post count greater than / equal to 3

  26. 26

    Excel: count numbers greater than 3 in strings in range of cells

  27. 27

    How to user a 'greater than' PHP statement with MySQL Decimals

  28. 28

    MySQL: Select TIMEDIFF values where the difference is greater than x

  29. 29

    MySQL: Select rows with values nearest but greater than a given value

HotTag

Archive