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 4172346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:33:08+00:00 2026-05-21T00:33:08+00:00

Suppose I have a table that logs incoming users where each user has an

  • 0

Suppose I have a table that logs incoming users where each user has an IP address (ipaddr).

What’s the best way to select all users that have never come to the site before? (so that particular IPADDR value only exists in the table once ), however I only want to know about the new visitors that came in the last 6 hours.

I basically want to do something like this in SQL:

SELECT * from visitors GROUP BY ipaddr WHERE COUNT(ipaddr) = 1 and date > '2011-03-31 00:59:11'

HOWEVER, the DATE condition should only apply to the results, and not for checking if the visitors is new or not.

UPDATE:

There is an SID field that accounts for user browsing sessions.

Here’s the relevant table schema:

CREATE TABLE `visitors` (
  `date` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `sid` bigint(12) unsigned NOT NULL,
  `ipaddr` int(8) NOT NULL,
)

Some example data:

INSERT INTO `visitors` (`date`,`sid`, `ipaddr`)
VALUES
    ('2011-03-31 06:25:48', 299521885457, -1454342140);


INSERT INTO `visitors` (`date`,`sid`, `ipaddr`)
VALUES
    ('2011-03-31 06:26:37', 299521885457, -1454342140);


INSERT INTO `visitors` (`date`,`sid`, `ipaddr`)
VALUES
    ('2010-01-01 15:23:44', 694387538590, -1454342140);

This visitor has two rows for his current session happening in realtime, each row is for each page he has visited (only relevant schema shown). The last example row shown is a visit from 2010, which means this ip address has 2 different SIDs belonging to it, therefore is not a new visitors.

The query’s result should have none of the rows listed above, seeing as how this visitor has two sessions in the database. If the last row is removed (with sid 694387538590), the visitor should become a new visitor and appear in the query.

  • 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-21T00:33:09+00:00Added an answer on May 21, 2026 at 12:33 am

    the "WHERE" for GROUP BY is HAVING:

    SELECT ipaddr from visitors
    GROUP BY ipaddr
    HAVING COUNT(ipaddr) = 1 AND MIN(date) > '2011-03-31 00:59:11'
    

    UPDATE

    SELECT ipaddr, max(sid) sid
      FROM visitors
     GROUP BY ipaddr
    HAVING     COUNT(DISTINCT sid) = 1
           AND MIN(date) > '2011-03-31 00:59:11'
    

    Explanation:

    SELECT date, sid, ipaddr FROM visitors
    
    date                sid        ipaddr 
    ------------------------------------------
    2011-03-31 06:25:48 299525457  -1454342140 
    2011-03-31 06:26:37 299525457  -1454342140 
    2010-01-01 15:23:44 694388590  -1454342140 
    2011-03-31 11:23:44 111111111  -1234444811 
    2011-03-31 12:23:44 111111111  -1234444811
    
    SELECT ipaddr FROM visitors GROUP BY ipaddr
    
    ipaddr
    -----------
    -1454342140 
    -1234444811 
    
    --- group for ip -1454342140 ---
    
    2011-03-31 06:25:48 299525457  -1454342140 
    2011-03-31 06:26:37 299525457  -1454342140 
    2010-01-01 15:23:44 694388590  -1454342140
    
    COUNT(DISTINCT sid) = COUNT(299525457, 694388590) = 2
    --> there is more than 1 session for this ip: not good!!!
    
      ==> group discarded
    
    --- group for ip -1234444811 ---
    
    2011-03-31 11:23:44 111111111  -1234444811 
    2011-03-31 12:23:44 111111111  -1234444811
    
    COUNT(DISTINCT sid) = COUNT(111111111) = 1 --> OK
    (here COUNT(sid) = count(111111111, 111111111) = 2
     --> despite it is the same sid, the count is 2, that is why using DISTINCT)
    
    MIN(date) = '2011-03-31 11:23:44' > '2011-03-31 00:59:11' --> OK
    
      ==> group accepted
    

    Authorized columns in the SELECT are:

    • columns used in the GROUP BY clause
    • agregates of the other columns

    ipaddr was used in GROUP BY but not sid. To have also sid I used MAX but remember that it will be applied only to the group of rows for current ipaddr and because of the conditions in the query there is 1 unique sid but repeated so the result will be that sid

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

Sidebar

Related Questions

Suppose I have a database table that has a timedate column of the last
Suppose I have a table called Companies that has a DepartmentID column. There's also
suppose that I have this RDBM table ( Entity-attribute-value_model ): col1: entityID col2: attributeName
Suppose I have a tags table with two columns: tagid and contentid . Each
Suppose we have a table A: itemid mark 1 5 2 3 and table
Suppose I have a table . Now, I'm interested in Getting Useful Data Easily.
Suppose I have a database table with two fields, foo and bar. Neither of
Suppose you have the tables Presentations and Events . When a presentation is saved
Suppose I have a dataset with those two immortal tables: Employee & Order Emp
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX

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.