Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5976329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:12:46+00:00 2026-05-22T21:12:46+00:00

I have a mysql table like with the following rows: id | event_type |

  • 0

I have a mysql table like with the following rows:

id  |  event_type  |  occurance_date  |  ipaddress

I want to get the days, unique visits and total visits / day. This is pretty easy to do with 2 queries but I want to make only one.

So i thought
To get the days i can

SELECT DISTINCT(occurance_date) as day FROM statistics WHERE event_type = 'visit' 
ORDER BY id DESC LIMIT 14;

To get unique visits / day

SELECT COUNT(DISTINCT(ipaddress)) as unique_visits FROM statistics WHERE occurance_date = '2011-05-11';

To get all visits / day

SELECT COUNT(id) as total_visits FROM statistics WHERE occurance_date = '2011-05-11';

The problem is how can I make a single query to return me the all the days, with unique_visits and total_visits.

(2011-05-11 should be replaced by date, it’s just an example)

Any suggestion is more then welcomed.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:12:46+00:00Added an answer on May 22, 2026 at 9:12 pm
    select count(*) as total_visits, count(distinct(s1.ipaddress)) as unique_visits 
    FROM statistics s1 
    WHERE event_type = 'visit'
    GROUP BY occurrence
    ORDER BY unique_visits;
    

    Annotated version:

    First we do a SELECT count(all rows)

    select count(*) as total_visits, 
    

    Plus a count of the distinct ipaddresses

    count(distinct(s1.ipaddress)) as unique_visits 
    

    From statistics, I’ve added an alias, but that’s just out of habit. It is not needed.

    FROM statistics s1
    

    Remember you wanted only rows that had event_type ‘visit’.
    You also had some information need about that, but you did not want to filter those out, you just wanted to see them as separate items in the result.
    So the only filter we have is event_type.

    WHERE event_type = 'visit'
    

    We group the result by occurrence date, if you leave this line out, you will only see one line. Put it back in to see the difference.

    GROUP BY occurrence
    

    And we order the results by number of visits, except I didn’t want to rewrite count(distinct(s1.ipaddress)) so I wrote the alias unique_visits; this is allowed (and even encouraged in an order by clause.)

    ORDER BY unique_visits;
    

    Links:
    Group by, never mind about this myth part, just read the description of how group by works:
    http://dev.mysql.com/tech-resources/articles/debunking-group-by-myths.html
    Count: http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in MySQL That looks like the following: date |storenum |views
I have a MySQL-InnoDB table with 350,000+ rows, containing a couple of things like
I have a (mysql) database table with the following columns: NAME | String (Unique)
I have a table in my MySQL database that looks like the following banner
I have a MySQL table like this: Payments +----+---------------------+---------+ | id | date (sorted
I have this mysql table called comments which looks like this: commentID parentID type
I have a MySQL table with a column of well-formed URLs. I'd like to
Let's say I have a MySQL table that is something like this: software table:
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a table in mysql like this Name Result T1_09_03_2010 fail T2_09_03_2010 pass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.