0

Hi I have this VISITS table

enter image description here

What I want to achieve:

**affiliate_id** **unique visits count** 

167               4
121               1
137               1

Special Condition is one IP can only be counted once per day for single affiliate_id.

So for visit_id 553 and 554, it can be only counted as one visits because both have same ip, same date and same affiliate_id.

From what I understand I need to group by ip, date and affiliate_id and count it, but not sure how to write the query.

Can you guys point me to some reference or insight to solve this problem?

Thanks in advance!

--

Update with link sample SQL:

https://dl.dropboxusercontent.com/u/3765168/tb_visits.sql

1

3 Answers 3

1

Based on your requirement i think you need the distinct ip per date and affiliate_id

 select DATE(date), affiliate_id, count(distinct( ip))
 from your_table
 group by DATE(date), affiliate_id
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but the query not grouping by affiliate_id as in picture below dl.dropboxusercontent.com/u/3765168/query1.png
Hi @Dario answer is the most correct, however your answer also give me something that I need, so I am giving you vote for your assistant. Thanks man!
0

If I understood correctly,

SELECT affiliate_id, count(*) 
  FROM (SELECT DISTINCT affiliate_id, ip, DAY(date)
          FROM visits) AS q
  GROUP BY affiliate_id;

3 Comments

Thanks but the query group all visits into one affiliate_id only as in picture below dl.dropboxusercontent.com/u/3765168/query2.png
Sorry I had been too hasty. Edited.
Thanks sir your query is exactly what I need after scrathing my head for a day before coming to StackOverflow :)
0

What you are trying to do is group the number of unique or distinct ip's for a given affiliate_id so the only group by you need is the affiliate_id. The Unique hits are calculated using a count and to make then unique you add the DISTINCT key word

SELECT 
    affiliate_id, COUNT(DISTINCT ip) AS unique_visit_counts, 
FROM tablename
GROUP BY affiliate_id

However since you want it by the day as well you might want to include a date clause such as:

DATE_FORMAT(date, "%y-%m-%d") AS `date` 

Which will turn your date and time stamp into a day in the YY-MM-DD format.

If you group by that you can get a full list by day by affiliate_id using something like

SELECT 
    affiliate_id, 
    COUNT(DISTINCT ip) AS unique_visit_counts, 
    DATE_FORMAT(date, "%y-%m-%d") AS `date`  
FROM tablename
GROUP BY `date`, affiliate_id

Or pick a specific date using something like

SELECT 
    affiliate_id, 
    COUNT(DISTINCT ip) AS unique_visit_counts, 
FROM tablename
WHERE DATE_FORMAT(date, "%y-%m-%d") = '17-02-08'
GROUP BY affiliate_id

4 Comments

Hi Dave thanks for your insight, I did try your query but it still not grouping by affiliate_id as in picture below dl.dropboxusercontent.com/u/3765168/notgroupingbyid.png I upload sample SQL with data for reference dl.dropboxusercontent.com/u/3765168/tb_visits.sql
Is there a reason why IP needs to be tinytext? can it be varchar(255) or better yet varchar(40). It seems like a waste if you're only storing an IP in there. Anyway DISTINCT doesn't work with text type fields in SQL so you'll have to either change it (preferred) or CAST it CAST(ip as varchar(255)) Haven't tested the code yet but something like ... COUNT(DISTINCT CAST(ip as varchar(255))) Might work?
Hi the IP tinytext is out of my control since it was a table from third party library / extension. I have accepted the other answer but your help is really appreciated. Thanks again sir for your kind soul.
np Best of luck to you. I'm glad that you found your answer but I will express a bit of my concern with the use of the DAY() function if you plan on having data that spans over a month. https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_dayofmonth Might cause some unexpected results, unless that's what you're going for. In which case I'm way in left field. DATE() maybe more what you're looking for with that answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.