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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:30:37+00:00 2026-06-12T00:30:37+00:00

I’m trying to write a query that returns a list of dates and the

  • 0

I’m trying to write a query that returns a list of dates and the DISTINCT COUNT of User IDs for the 7 days preceding each date. The table I’m working with is simple, and looks like this:

Started                 UserId
"2012-09-25 00:01:04"   164382
"2012-09-25 00:01:39"   164382
"2012-09-25 00:02:37"   166121
"2012-09-25 00:03:35"   155682
"2012-09-25 00:04:18"   160947
"2012-09-25 00:08:19"   165806

I can write the query for output of an individual COUNT as follows:

SELECT COUNT(DISTINCT UserId)
FROM Session 
WHERE Started BETWEEN '2012-09-18 00:00' AND '2012-09-25 00:00';

But what I’m trying to do is output this COUNT for every day in the table AND the 7 days preceding it. To clarify, the value for September 25th would be the count of DISTINCT User IDs between the 18th and 25th, the 24th the count between 17th and 24th, etc.

I tried the following query but it provides just the COUNT for each day:

SELECT 
DATE(A.Started),
Count(DISTINCT A.UserId)
FROM Session AS A
WHERE DATE(A.Started) BETWEEN DATE(DATE_SUB(DATE(DATE(A.Started)),INTERVAL 7 DAY)) AND DATE(DATE(A.Started))
GROUP BY DATE(A.Started)
ORDER BY DATE(A.Started);

And the output looks like this:

DATE(A.Started) "Count(DISTINCT A.UserId)"
2012-09-18          709
2012-09-19          677
2012-09-20          658
2012-09-21          556
2012-09-22          530
2012-09-23          479
2012-09-24          528
2012-09-25          480
...

But as I said, those are just the daily counts. Initially I thought I could just sum the 7 day values, but that will invalidate the DISTINCT clause. I need the DISTINCT UserId counts for each 7 day period preceding a given date.

  • 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-12T00:30:38+00:00Added an answer on June 12, 2026 at 12:30 am

    This query should work for you:

    SELECT
        DATE_FORMAT(d1.Started, '%Y-%m-%d') AS Started,
        COUNT(DISTINCT d2.UserID) Users
    FROM
    (
        SELECT
            DATE(Started) AS Started
        FROM
            Session
        GROUP BY
            DATE(Started)
    ) d1 
    INNER JOIN
    (
        SELECT DISTINCT
            DATE(Started) AS Started,
            UserID
        FROM
            Session
    ) d2
    ON d2.Started BETWEEN d1.Started - INTERVAL 7 DAY AND d1.Started
    GROUP BY
        d1.Started
    ORDER BY
        d1.Started DESC
    

    Visit http://sqlfiddle.com/#!2/9339c/5 to see this query in action.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.