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

The Archive Base Latest Questions

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

I have a Payment table that looks a little like this: Id (int identity)

  • 0

I have a Payment table that looks a little like this:

Id (int identity)
CustomerId (int)
PaymentDate (SmallDateTime)

Now I want to write a query that will find those customers that have made three payments within a period of three months. Given the following data:

Id   CustomerId   PaymentDate (YYYY-MM-DD)
------------------------------------------
1    1            2010-01-01
2    1            2010-02-01
3    1            2010-03-01
4    1            2010-06-01
5    2            2010-04-01
6    2            2010-05-01
7    2            2010-06-01
8    2            2010-07-01

I would like to produce the following result:

CustomerId    LastPaymentDateInPeriod
-------------------------------------
1             2010-03-01
2             2010-07-01

Where LastPaymentDateInPeriod is the PaymentDate with the highest value within a three-month period. If there is more than one three-month period for a given customer it would have to return the highest value from the most recent period (this is what I tried to illustrate for customer 2 in the above example). Note that three payments on three consecutive days would also meet the criteria. The payments just have to fall within a three-month period.

I know how to do this with a cursor and a lot of smaller queries but this is slow (and, I’ve come to understand, should only be a last resort). So do any of you SqlServer geniuses know how to do this with a query?

Thanks in advance.

  • 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-19T11:17:27+00:00Added an answer on May 19, 2026 at 11:17 am

    This should do the job:

    select 
        CustomerID,
        max(LastPaymentDateInPeriod) as LastPaymentDateInPeriod
    from
    (
        select
          LastPaymentInPeriod.CustomerID,
          LastPaymentInPeriod.PaymentDate as LastPaymentDateInPeriod
        from Payment LastPaymentInPeriod
          inner join Payment RelatedPayment on
            LastPaymentInPeriod.CustomerID = RelatedPayment.CustomerID and
            LastPaymentInPeriod.PaymentDate > RelatedPayment.PaymentDate and
            datediff(m, RelatedPayment.PaymentDate, LastPaymentInPeriod.PaymentDate) < 3
        group by
          LastPaymentInPeriod.CustomerID,
          LastPaymentInPeriod.PaymentDate
        having
          count(*) > 1
    
     ) as PaymentPeriods
    group by
        CustomerID
    

    update: I’ve tested this now and it seems to work for @Martin’s data

    update2: If it’s a requirement that Jan 31 and Apr 1 should be considered as less than 3 months apart then the DATEDIFF function call can be replaced with something like this:

    create function fn_monthspan
    (
        @startdate datetime,
        @enddate datetime
    )
    returns int
    as
    begin
        return datediff(m, @startdate, @enddate) - case when datepart(d, @startdate) > datepart(d, @enddate) then 1 else 0 end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a base table that looks something like this. SELECT [BILL_MONTH] ,[BILL_YEAR]
Let us say that I have a table structured like this(using SQL server): empID
I have a MySQL table like this: Payments +----+---------------------+---------+ | id | date (sorted
I have two tables in a teradata database that look like this accounts account_number
I have two models that I would like to save in the same table.
This seems like such an obvious thing that excel must have this feature, I
I have a list of payment transactions in a table. That include payment reversals.
lets say I have a table call payment that have a field call thing_id
I have a string coming from a table like can no pay{1},as your payment{2}due
I have a table named PAYMENT . Within this table I have a user

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.