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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:26:48+00:00 2026-05-21T11:26:48+00:00

I need to segment clients by the first date that their count of orders

  • 0

I need to segment clients by the first date that their count of orders reached a specific value.

for example, we have a table of orders

ID, ClientID, Date

1,  1, 1/1/2011
2,  1  2/1/2011
3,  1  3/1/2011
4,  1  4/1/2011

I would like to get all the clients who has at least 3 orders and get the Date field when they reached 3 orders.
on my example, the query will show:

ClientID: 1, Date: 3/1/2011

(cause on the 3/1/2011, the client reached 3 orders)

How can I do it?

Thanks

Update:

I’m looking at your solutions (thank you very much!) but I need to do the same thing also with SUM (lets say the table above has a field called amount and I would like to segment the clients and get the first date that the client orders reached to $100)
I though that the solutions will help me understand the logic and easily convert the count to sum and make a new query but I got little bit lost here.. any ideas?

  • 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-21T11:26:49+00:00Added an answer on May 21, 2026 at 11:26 am

    This would select all the clients with 3 orders with the date of 3rd order.

    --DECLARE @clientID INT
    --SET @clientID = 1
    DECLARE @count INT
    SET @count = 3      -- Set the count to this variable.
    
    SELECT  ClientID, [Date]
    FROM    [yourtable] a
    WHERE   COALESCE(    (SELECT COUNT(*) 
                          FROM [yourtable] b 
                          WHERE b.[Date] < a.[Date] and b.ClientID = a.ClientID ),0) = @count - 1
    ORDER BY ClientID
    

    To select single client with date of 3rd order where could be modified with additional condition for that.

    AND a.ClientID = @clientID
    

    UPDATE based on question update – This query with little modification from count woud give you the running sum selection. There are few more techniques listed here for running sum.

    DECLARE @sum INT
    SET @count = 100      -- Set the amount to this variable.
    
    SELECT  ClientID, [Date]
    FROM    [yourtable] a
    WHERE   ( a.Amount + COALESCE(    (SELECT SUM(b.Amount) 
                          FROM [yourtable] b 
                          WHERE b.[Date] < a.[Date] and b.ClientID = a.ClientID ),0) ) = @sum
    ORDER BY ClientID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a url, - http://example.com/sales/view/id/705 and I need get a last segment (705).
I have a ray, I need to find the closest line segment that it
I have a line segment (x1, y1), (x2, y2) that i need mirrored so
I need to limit an angle so it fits into a segment. I have
I have a segmented liver. I need to segment the tumor in it. I
I need some help creating a query. Heres a sample table: Segment Name 1
I have a list of video segment durations I need to add up to
I have some C++ code (segment seen below), I need to convert this to
So basically the problem is that I have segment of code that needs to
I need to add some sort of line break after each segment (date, name,

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.