MYSQL COUNT WITH 3 DISTINCT - mysql

Hi I have this VISITS table
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

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

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

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

Related

Distinct after grouping by

I have below query as
WITH search_agg as(
SELECT user_id, count(search_id) as count_search
FROM search
WHERE date>current_date -interval '7 days'
GROUP BY user_id
HAVING count_search>10)
SELECT count (distinct user_id)
FROM search_agg
If am correct, I don't need distinct in my outer query since my group by takes care of that correct? or is it better practice to have distinct anyway? Thanks
It depends upon your need in this case there is no need for distinct. Because CTE already returns (distinct) group by user_id.
WITH search_agg as(
SELECT user_id, count(search_id) as count_search
FROM search
WHERE date>current_date -interval '7 days'
GROUP BY user_id
HAVING count_search>10)
SELECT count (user_id)
FROM search_agg;
If you have group by based on two or more columns then you need to use distinct to fetch unique rows.

Find a column with one value for 2 different dates

I have the following table:
table 1
columns are as following:
date , time , user_id , channel
I wish to find for a list of USERS watching in 2 different DATES , channel(lets say CNN, NBC...) all the entries relevant.
Means the channel in DATE 1 and DATE 2 is the same, also the user_id.
I try allready following:
select distinct monthname(date),max(date), min(date) count(distinct user_id)
from iptv_clean
group by monthname(date)
having min(date)!= max(date)
But it seems not to work well.
Any ideas?
The following gives a list of users and channels that users watched on more than one date:
select user_id, channel, count(distinct date)
from iptv_clean
group by user_id, channel
having count(distinct date) > 1;
Is this what you want?

MySQL Query: Get new customers in the month

I have millions of records in a table with following columns:
id, item_id, customer_id, date
I want to find customer_id for a specific month which are inserted first time in the table in that month.
What is best and simple query for this.
Thanks
select customer_id , min(date) as min_date
from theTable
group by customer_id
having min_date>=<the desired date>
try something like:
SELECT
date_format(date,"%M") month,group_concat(customer_id ORDER BY customer_id) customer_ids
FROM
<table_name>
GROUP BY month;
Something like this may be:
select distinct(id) from customer
where month(date)='Your_month'
order by date

MySql subquery: average difference, grouped by column

I have a mysql table with visitor_id, country, time_of_visit.
I want to get the average duration of visit by country.
To get duration, I get the difference between the earliest and latest time_of_visit for each visitor_id.
So this gets me the average duration of all visits:
SELECT AVG(duration)
FROM
(
SELECT TIMESTAMPDIFF(SECOND, MIN(time_of_visit), MAX(time_of_visit))/60
as duration
FROM tracker
GROUP BY visitor_id
) as tracker
That works. But to group it by country, I fail. Here's my most recent attempt to get average duration by country:
SELECT country, AVG(duration)
FROM
(
SELECT TIMESTAMPDIFF(SECOND, MIN(time_of_visit), MAX(time_of_visit))/60
as duration
FROM tracker
GROUP BY visitor_id
) as tracker
GROUP BY country
The error I get is: Unknown column 'country' in 'field list'.
I think this should be simple, but I'm a noob. I searched a lot, tried lots of ideas, but no good. Any help?
Thanks in advance.
You have to select the country column in your subquery and then have to reference the country field from the derived table tracker.country
SELECT tracker.country, AVG(tracker.duration)
FROM
(
SELECT TIMESTAMPDIFF(SECOND, MIN(time_of_visit), MAX(time_of_visit))/60
as duration ,country
FROM tracker
GROUP BY visitor_id
) as tracker
GROUP BY tracker.country
Edit
Using GROUP BY in subselect visitor_id will gives you the record
with duplicate data for countries and when using GROUP BY with both
visitor_id,country will group the data of countries within the same
visitor id, this will only possible if one visitor will belong to more
than one countries , if one visitor will belong to only one country
i.e one-to-one relation then just use GROUP BY visitor_id
You need to specify the column in the subquery which you want to show it in the outer query
Try this::
SELECT country, AVG(duration)
FROM
(
SELECT TIMESTAMPDIFF(SECOND, MIN(time_of_visit), MAX(time_of_visit))/60
as duration, country
FROM tracker
GROUP BY visitor_id
) as tracker
GROUP BY country
You can also try:
SELECT
country,
AVG(TIMESTAMPDIFF(SECOND, MIN(time_of_visit), MAX(time_of_visit))/60) as avgTime
FROM
GROUP BY visitor_id,country
You should add COUNTRY to the inner GROUP BY and a field list
SELECT country, AVG(duration)
FROM
(
SELECT TIMESTAMPDIFF(SECOND, MIN(time_of_visit), MAX(time_of_visit))/60
as duration,country,visitor_id
FROM tracker
GROUP BY visitor_id,country
) as tracker
GROUP BY country

mysql query for website visiter count

I have a table to store website visiter count bellow
id date ip
-----------------------------
1 1-1-2012 195.165.1.2
2 1-1-2012 195.165.1.3
3 12-2-2012 195.165.1.8
and i want a mysql query for get count of each month of current year
seeing your DATE column, i assumed that you save dates as VARCHAR
SELECT MONTHNAME(STR_TO_DATE(date, '%c-%e-%Y')) `monthName`, COUNT(*) totalCount
FROM tableName
WHERE YEAR(STR_TO_DATE(date, '%c-%e-%Y')) = YEAR(CURDATE())
GROUP BY MONTHNAME(STR_TO_DATE(date, '%c-%e-%Y'))
SQLFiddle Demo
OTHER SOURCEs
STR_TO_DATE()
YEAR()
CURDATE()
MONTHNAME()
"select month(date),count(distinct ip) from visitors where date>='$y-01-01' and date<'$y-12-31' group by 1"
SELECT YEAR(date) YEAR,MONTHNAME(date) MONTH,count(id) TOTAL
FROM table_name
GROUP BY YEAR(date),MONTH(date)
THE QUERY IS LIKE THIS BECAUSE DIFFERENT PERSONS FROM SAME IP MAY VISIT