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

  • Home
  • SEARCH
  • 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 6081535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:11:12+00:00 2026-05-23T11:11:12+00:00

I have a table that records all user cross-profile visits and I need to

  • 0

I have a table that records all user cross-profile visits and I need to get the count of distinct visits and the count of new visits to a specified profile (:user_id) since the specified UNIX timestamp (:time).

id  | user_id  | visitor_id |  time (unix)
====+==========+============+=============
  1 |     1    |       5    |  1300000000
  2 |     1    |       5    |  1300000010
  3 |     1    |       8    |  1300000015
  4 |     1    |       9    |  1300000020
  5 |     1    |       9    |  1300000025
  6 |     1    |       9    |  1300000030

So far I was only able to get the count of total visits, but am unable to get the new ones.

SELECT COUNT(DISTINCT v.visitor_id) AS total, SUM(v.time > 1300000012) AS new FROM profile_visits v WHERE v.user_id = 1

returns

total | new
============
   3  |   4

but the required result is

total | new
============
   3  |   2
  • 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-23T11:11:13+00:00Added an answer on May 23, 2026 at 11:11 am

    You probably want SUM(v.time > 1300000012) AS new.

    The expression is either 0 or 1. COUNT() will count 0 as happily as 1. But SUM() will “count” only the 1’s.


    Re your comment:

    SELECT COUNT(t.visitor_id) AS total, SUM(t.subtotal_new) AS total_new
    FROM (SELECT v.visitor_id, SUM(v.time > 1300000012) AS subtotal_new
      FROM profile_visits AS v GROUP BY v.visitor_id) AS t
    

    The SUM() of the inner query counts new visits per visitor. The SUM() of the outer query gives the total new visits for all visitors.

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

Sidebar

Related Questions

How to select all records from one table that have corespondent keys with other,
Say you have a ServiceCall database table that records down all the service calls
I have a User table that has a many records to 1 user relationship.
I have a table that records when a user logs in. I want to
I have MySQL table that contains all messages from user to user: id |
I have a table that has records with a structure similar to this.. ID
I have a table that contains records with a column indicates the Date. Given
I have one table that has sales records and another table that has additional
I have in my table records that are related one to another by a
I have a table that has millions of records and we are looking at

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.