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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:04:19+00:00 2026-06-05T02:04:19+00:00

I have a dataset that analogously looks like this: X | U | datetime

  • 0

I have a dataset that analogously looks like this:

X | U | datetime
-------------
1 | 1 | 1/1/12
1 | 2 | 1/1/12
1 | 2 | 1/1/12
1 | 2 | 1/1/12
1 | 4 | 1/1/12
2 | 2 | 2/1/12
2 | 3 | 2/1/12
1 | 3 | 3/1/12
2 | 4 | 3/1/12
3 | 2 | 4/1/12

it is a log of visits. X is the id of the thing visited, and U is the user id

I need to compute two statistics.

Given a value for X (x):

  1. "new visitors": count the number of unique users that made their first visit for any X to x.

use cases:

  • A user has only visited x once -> counts as 1
  • A user has only visited !x once -> counts as 0
  • A user has only visited x twice -> counts as 1
  • A user has only visited !x twice -> counts as 0
  • A user has visited many Xs, where their first visit of any X is x -> counts as 1
  • A user has visited many Xs, where their first visit of any X is !x -> counts as 0

Examples from above data:

X | Count
---------
1 | 3
2 | 1
3 | 0
  1. "returning visitors": count the number of unique users that have visited x more than once OR have visited x once, but have visited another X previously (i.e. visits made after their single visit to x do not count)

Examples from above data:

X | Count
---------
1 | 3
2 | 2
3 | 1 

I’m using SQL Server 2008.

Update

This appears to answer Q1, although it’s not very fast 🙁

select x.X, COUNT(1)
from (
    select t1.X
    from @t t1
    group by t1.X, t1.U
    having (select COUNT (1) from @t t2 where t2.u= t1.U and t2.OccurredOn < MIN(t1.OccurredOn)) =0 
) x 
group by x.X

Update 2

I think this is (2)

select t.X, COUNT(1)
from @t t
left join (
    select t.U, MIN(t.OccurredOn) as O
    from @t t
    group by t.U
) x on t.U = x.U and t.OccurredOn <= x.O
where x.U is null
group by t.X
  • 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-05T02:04:20+00:00Added an answer on June 5, 2026 at 2:04 am

    For the first case, you need a sub-query to join to that will filter out all user-thing visits that aren’t the first of their kind. So you’ll have something like

    select X, count(*) [First Visits]
    from table t1
         join (select U, min(datetime) firstvisit 
               from table 
               group by U) t2 on t1.datetime = t2.firstvisit and t1.U = t2.U
    group by X
    

    Edit: I think your solution to the second one is fine, except that the join will be faster if you replace the <= with simply =.

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

Sidebar

Related Questions

i have a dataset that looks like this: Bin Frequency 6.0 0 5.9 0
I have a dataset that looks like this: a <- data.frame(rep(1,5),1:5,1:5) b <- data.frame(rep(2,5),1:5,1:5)
I have a dataset that looks like this: 0 _ _ 23.0186E-03 10 _
SQL 2008 Basically i have a dataset that looks like this: AcctID AcctType AcctSubType
I have a dataset that looks like this: 1 -0.151714363660730E+03 0.681572558518519E+02 -0.147787110884357E+03 0.702453634941157E+02 -0.147765104000000E+03
I have a dataset that looks like this: Account Cost Centre TransNo aaa 111
I have a dataset that looks like this: CATA 1 10101 CATA 2 11101
i have a dataset that looks like this id, value1,value2,value3,value4,value5...value200 (i am showing only
I have a dataset that looks like so: id entry_id the_data 1 100 Z
i have text file that looks like this: 1 \t a 2 \t b

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.