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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:52:52+00:00 2026-06-01T07:52:52+00:00

I have a table and I need to count all unique users on it.

  • 0

I have a table and I need to count all unique users on it. The schema of the table is:

idAccess | idClient | date             | uid
1        | 12       | 2012-04-03 10:59 | 1234-1234-1234
2        | 13       | 2012-01-03 11.23 | 2345-2345-2345
3        | 12       | 2012-04-03 10:59 | 1234-1234-1234
4        | 12       | 2012-04-03 11:59 | 1234-1234-1234
5        | 12       | 2012-02-23 02:39 | 5788-5788-5687
6        | 12       | 2011-12-03 12:31 | asdf-1234-asdf
7        | 12       | 2011-10-13 13:36 | eeef-1234-eeee
8        | 15       | 2010-11-23 17:33 | qwer-EeQE-fhjh

This is the query:

Select 
    count(*) AS ct, count(DISTINCT(uid)) as users, YEAR(date) as ano 
FROM 
    [AQUA_INTRANET].[dbo].[Accesos] 
WHERE 
    idClient IN (12,13,15) 
    AND date> '01-01-2006 00:00' AND date< '04-02-2012 23:59'   
GROUP BY 
    YEAR(date) 
ORDER BY 
    year

and the result:

ct | users  | year
5  |   6    |  2012
2  |   3    |  2011
1  |   1    |  2010 

If I execute this query, I’m getting an wrong results, because the sum of the results is different from this other query:

Select 
     count(DISTINCT(uid)) as users
FROM 
     [AQUA_INTRANET].[dbo].[Accesos] 
WHERE  
     idCliente IN (12,13,15) 
     AND fecha > '01-01-2006 00:00' 
     AND fecha < '04-02-2012 23:59'

and result is:

users
 6

that is different if you sum the users column on the other query.

This happens sometimes, not always, but it’s very strange, because I’m having different values if I group by year or if I get the sum of all unique users.

I don’t know where is the problem, and I tried with different queries, but the result it wrong if I group by year.

  • 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-06-01T07:52:53+00:00Added an answer on June 1, 2026 at 7:52 am

    One uid can have records that are in more than one year.

    Try these three queries

    SELECT count(DISTINCT(uid)) as users
      FROM [AQUA_INTRANET].[dbo].[Accesos] 
     WHERE idCliente IN (12,13,15)
       AND fecha >= '01-01-2010 00:00' AND fecha < '01-01-2011'
    
    SELECT count(DISTINCT(uid)) as users
      FROM [AQUA_INTRANET].[dbo].[Accesos] 
     WHERE idCliente IN (12,13,15)
       AND fecha >= '01-01-2011 00:00' AND fecha < '01-01-2012'
    
    SELECT count(DISTINCT(uid)) as users
      FROM [AQUA_INTRANET].[dbo].[Accesos] 
     WHERE idCliente IN (12,13,15)
       AND fecha >= '01-01-2012 00:00' AND fecha < '04-02-2012 23:59'
    

    Also, note that I use >= AND < to ensure no records ‘fall through the gaps’ or get double counted.

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

Sidebar

Related Questions

I have this table structure: CREATE TABLE users ( uid bigint NOT NULL, first_name
I have a table with field1, field2, field3, … and I need to count
I have 3 tables namely: Town; Village; Farmers...and I need to COUNT how many
I have three table I need to query table 1,2 and 3 and select
I have table 'products' and I need to update 'price' field by 20% if
I have table to test score data that I need to pivot and I
I have table: id name type where type is 1 or 2 I need
I have table which contains double values stored in mysql database ...I need to
I have table A in Oracle that has a primary key (id). I need
I have table with an auto-increment field, but I need to transfer the table

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.