1

I have a view called 'videos' with the following columns:

email(varchar)
time(varchar)
page(varchar)

The view has rows for every time a person with a given email visited a page.

I want to return results that show for each email address, a count of how many days they visited at least one page, and a count of how many distinct pages they visited regardless of date.

One of my issues is probably that the time is stored as a varchar. Here is an example of the format the time is in.

2016-01-25T14:36

0

2 Answers 2

3

Use STR_TO_DATE to extract and interpret the date-portion of your time string. Use COUNT with the DISTINCT keyword to count only distinct dates and distinct pages.

SELECT email
     , COUNT(DISTINCT STR_TO_DATE(time, '%Y-%m-%d')) AS visit_days
     , COUNT(DISTINCT page) AS visit_pages
  FROM videos
 GROUP BY email
;
Sign up to request clarification or add additional context in comments.

1 Comment

That works perfectly, thanks! I wasn't sure how to do two separate counts in a single query
0

You can use a group by fro the count of the visit by day

select email,  date(time), count(*)
from mytable 
group by email, date(time);

and

select email,   count(distinct page)
from mytable 
group by email;

Comments

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.