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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:13:10+00:00 2026-06-01T19:13:10+00:00

I have an event record database, I need to find all events for each

  • 0

I have an event record database, I need to find all events for each costumer and group them in one week interval from the last record of each costumer until the first.

FirstEvent                  LastEvent               Client
==
2011-01-01 06:55:21.000 |2011-04-30 21:46:09.000    |Client1
2011-01-01 06:20:00.000 |2011-02-23 12:43:55.000    |Client2
2011-01-03 08:34:33.000 |2011-04-30 09:00:39.000    |Client3
2011-01-01 02:14:45.000 |2011-04-30 15:31:41.000    |Client4
2011-01-01 08:08:12.000 |2011-02-21 09:41:28.000    |Client5
2011-02-01 11:29:28.000 |2011-04-29 09:13:25.000    |Client6

Now I have to count the records of each client and group them like this

Client     From                    To                        Records
==
Client1   |2011-04-30 21:46:09.000 |2011-04-23 21:46:09.000  |200
Client1   |2011-04-16 21:46:09.000 |2011-04-09 21:46:09.000  |400
                                 ...Until the the first date
Client1   |2011-01-08 06:55:21.000 |2011-01-01 06:55:21.000  |250
Client2   |2011-02-23 12:43:55.000 |2011-02-16 12:43:55.000  |50
                                 ...The same for each user

I hope someone can help me find a way to output this.

  • 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-01T19:13:12+00:00Added an answer on June 1, 2026 at 7:13 pm

    Okay, I think this does exactly what you wish for:

    SELECT CONVERT(datetime, '2011-01-01 06:55:21.000', 120) AS event, 'client1' AS Client
    INTO #EVENTS
    UNION ALL SELECT '2011-01-08 06:55:20.000', 'client1'
    UNION ALL SELECT '2011-04-30 21:46:09.000', 'client1'
    
    
    ;WITH E AS (
        SELECT
            Client,
            MIN(event) AS FirstEvent
        FROM #EVENTS
        GROUP BY Client
    )
    ,B AS (
        SELECT
            E.Client,
            E.FirstEvent,
            COUNT(*) AS Records,
            DATEDIFF(DAY, 0, c.event - E.FirstEvent) / 7 AS [weeks]     
        FROM #EVENTS c
        JOIN E ON E.Client = c.Client
        GROUP BY E.Client, E.FirstEvent, DATEDIFF(DAY, 0, c.event - E.FirstEvent) / 7
    )
    SELECT
        Client,
        DATEADD(WEEK, weeks, FirstEvent) AS [From],
        DATEADD(WEEK, weeks + 1, FirstEvent) AS [To],
        Records
    FROM B
    ORDER BY Client, weeks;
    
    DROP TABLE #EVENTS
    

    Though, if there are no records a certain week, it will leave out that row instead of returning a row containing the Records value of 0:

    Edit:

    If you wonder why I use DAY instead of WEEK for my DATEDIFF, it is to avoid problems whether Sunday or Monday is the first day of the week.

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

Sidebar

Related Questions

I have a database that will store events. Each event has different activities. The
I have button, which fires an event, that deletes a record from the database.
i have a php loop which displays only one record even if there is
I have a Cocoa application that records datestamps on events. I need to know
I have event manager process that dispatches events to subscribers (e.g. http_session_created, http_sesssion_destroyed). If
Input rz value In the head I have: Event.observe(window, 'load', function() { $$(input).each(function(field){ Event.observe(field,
Our application is modulraized (Group of modules doing specific things). The modules have event
I have 2 event handlers: Y.all(.ptl).on(mouseover, handleOverlay); Y.all(.ptl).on(mouseout, handleOverlay); And I would like to
I have an calendar event with StartDateTime and EndDateTime. These events are stored as
I've created this ASP.Net application and need to record what the users have done

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.