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
Related
I have table T
SELECT country, count(*) ,max(upated_date) from T
GROUP BY country
This will give me count of records and latest update date by country.
How to get count of records on previous latest updated date by country?
note: updated date is different for each country
basically I want like this
If you want the second last update date, then use window functions:
SELECT country, count(*),
max(updated_date),
max(case when seqnum = 2 then updated_date end) as penultimate_updated_date
FROM (SELECT t.*,
RANK() OVER (PARTITION BY country ORDER BY updated_date DESC) as seqnum
FROM T
) t
GROUP BY country
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
I have a very simple table phone_calls with these columns:
id, timestamp, phone_number
How to write a query which returns a daily count of returning users? It should have two columns:
Date, Number of Returning Users
Returning user:
A returning user for any given day D is the one, who has called at least once before D.
A user who has called multiple times on D, but hasn't called before D won't be counted as a returning user.
UPDATE
So here is what I have tried:
SELECT DATE(timestamp) AS date, COUNT(DISTINCT phone_number) AS user_count
FROM phone_calls
WHERE phone_number IN (SELECT phone_number FROM phone_calls GROUP BY phone_number HAVING COUNT(consumer_id) > 1)
GROUP BY DATE(timestamp)
But it's not a correct solution, because it doesn't comply with definition of Returning User mentioned above.
What I am struggling with?
For any given date, how do I filter out those phone numbers from the count, who never dialed in before that day?
SELECT
DATE(timestamp) AS date,
COUNT(DISTINCT phone_number) AS user_count
FROM
phone_calls pc
WHERE EXISTS (
SELECT *
FROM phone_calls pc1
WHERE
pc1.phone_number = pc.phone_number AND
DATE(pc1.timestamp) < DATE(pc.timestamp)
)
GROUP BY DATE(pc.timestamp)
Updated try this query
Select date(pc.timestamp) AS RDate ,count(*)
from phone_calls pc INNER JOIN phone_calls pcc
on pcc.phone_number=pc.phone_number
and date(DATE_ADD(pcc.timestamp, INTERVAL -1 DAY))= DATE (pc.timestamp) group by DATE (pc.timestamp);
This code works great for getting both a summary of individual Group counts and a Grand Total combined, but I only want the Grand Total of individual countries with no ROLLUP:
SELECT country, count( * )
FROM mytable
GROUP BY country
WITH ROLLUP
LIMIT 0 , 300
Researching a lot of examples, I was hoping this one would finally work but no joy (can't figure out what the MySQL error is in the code):
SELECT country,COUNT(*)
FROM mytable
GROUP BY country
COMPUTE Sum(Count(*))
Thanks for any assistance!
-stucko
If you want to get the number of rows returned by a GROUP BY you can do something like this
SELECT COUNT(*) FROM (
SELECT country, count( * )
FROM mytable
GROUP BY country
) a /*derived tables need an alias*/
I have a column Date with the same name in four tables Products, Customers, Shops, World in mysql. what i want to do is that i want to find the greatest date in the DATE COLUMN of all four tables. I don't want the greatest date of every table but the greatest one in all four tables. I hope you are understanding. Please help me
The most efficient way I can think of would be using the greatest function:
select greatest(
(select max(date) from products),
(select max(date) from customers),
(select max(date) from shops),
(select max(date) from world)
) greatest_date
SQLFiddle demo
This might work:
SELECT MAX(Date) as Max_Date
FROM (
SELECT Date FROM Products
UNION
SELECT Date FROM Customers
UNION
SELECT Date FROM Shops
UNION
SELECT Date FROM World
) AS P;