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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:44:58+00:00 2026-05-17T00:44:58+00:00

Please help me to generate the following query. Say I have customer table and

  • 0

Please help me to generate the following query. Say I have customer table and order table.

Customer Table

CustID CustName

1      AA     
2      BB
3      CC
4      DD  

Order Table

OrderID  OrderDate          CustID
100      01-JAN-2000        1  
101      05-FEB-2000        1     
102      10-MAR-2000        1 
103      01-NOV-2000        2    
104      05-APR-2001        2 
105      07-MAR-2002        2
106      01-JUL-2003        1
107      01-SEP-2004        4
108      01-APR-2005        4
109      01-MAY-2006        3 
110      05-MAY-2007        1  
111      07-JUN-2007        1
112      06-JUL-2007        1 

I want to find out the customers who have made orders on three successive months. (Query using SQL server 2005 and 2008 is allowed).

The desired output is:

CustName      Year   OrderDate   

    AA        2000  01-JAN-2000       
    AA        2000  05-FEB-2000
    AA        2000  10-MAR-2000

    AA        2007  05-MAY-2007        
    AA        2007  07-JUN-2007        
    AA        2007  06-JUL-2007         
  • 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-17T00:44:59+00:00Added an answer on May 17, 2026 at 12:44 am

    Edit: Got rid or the MAX() OVER (PARTITION BY ...) as that seemed to kill performance.

    ;WITH cte AS ( 
    SELECT    CustID  ,
              OrderDate,
              DATEPART(YEAR, OrderDate)*12 + DATEPART(MONTH, OrderDate) AS YM
     FROM     Orders
     ),
     cte1 AS ( 
    SELECT    CustID  ,
              OrderDate,
              YM,
              YM - DENSE_RANK() OVER (PARTITION BY CustID ORDER BY YM) AS G
     FROM     cte
     ),
     cte2 As
     (
     SELECT CustID  ,
              MIN(OrderDate) AS Mn,
              MAX(OrderDate) AS Mx
     FROM cte1
    GROUP BY CustID, G
    HAVING MAX(YM)-MIN(YM) >=2 
     )
    SELECT     c.CustName, o.OrderDate, YEAR(o.OrderDate) AS YEAR
    FROM         Customers AS c INNER JOIN
                          Orders AS o ON c.CustID = o.CustID
    INNER JOIN  cte2 c2 ON c2.CustID = o.CustID and o.OrderDate between Mn and Mx
    order by c.CustName, o.OrderDate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me figure a single query that will transform the data below... |id
Please help I am getting the following error when I am binding the drop
Please help me out with an algorithm for the following problem - Given a
Please help me about this issue... In my application i have calender where user
I have used the following code to generate some dynamic checkboxes. This works for
I have a requirement to run commands in the following order to automate the
I have created the following trigger for insert in the deleted user table the
How can i generate multiple texts using the imagecreatetruecolor() method? I have the following
I have the following table: id | billingno | location ------------------------- 1 | 9999999
Can you please help me with deciding which one of the following scenarios will

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.