I need a query which will get number of downloads for file. Table is like this
fid uid ip
21 0 111.111.111.11
21 0 222.222.222.22
21 0 111.111.111.11
21 1 333.333.333.33
21 1 111.111.111.11
21 1 444.444.444.44
21 2 555.555.555.55
22 0 111.111.111.11
uid is user id and if it’s 0 user is anonymous.
Query should count number of rows where fid is 21, but with distinct values of ips if users are anonymous and distinct values of users AND ips if they are registered.( If user download a file, logs out, and download again as anonymous, query should count this as 1 download)
In this example, query should count something like this
fid uid ip
21 0 111.111.111.11
21 0 222.222.222.22
21 1 333.333.333.33
21 2 555.555.555.55
and result should be 4.
Any help is much appreciated.
EDIT: I strikethrough some description, because it’s confusing.
Thanks for all the comments and possible solutions.
This query gets the grouping you want, except that I don’t know the business rule that determined
333.333.333.33should be returned foruid=1. I opted to useMAX(ip)instead:SQL Fiddle Example #1
You can then wrap it in a count, like this:
SQL Fiddle Example #2