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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:55:50+00:00 2026-06-12T17:55:50+00:00

I am having trouble writing this query. I need to get the current number

  • 0

I am having trouble writing this query.

I need to get the current number of orders that were shipped in consecutive months.

Example: if the current month is November and they placed orders in July, August, September, October, November, it would return 5 for that user. If they didn’t place an order in November, it would return 0 because their streak is broken.

The tables I’m concerned with are customer, order, and 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-12T17:55:52+00:00Added an answer on June 12, 2026 at 5:55 pm

    Use a cross join between the date table and the customer table to get a row for every customer / month combination and then left join that against the order table to get the details, using group by to get the counts.

    Something like this, although you will need to modify it to cope with the column names being reserved words.

    SELECT customer.name, month.name, COUNT(order.id)
    FROM customer
    CROSS JOIN date
    LEFT OUTER JOIN order
    ON customer.id = order.customer_id
    AND MONTH(date.date) = MONTH(order.date)
    WHERE date.date BETWEEN startofdaterange AND endofdaterange
    GROUP BY customer.name, month.name
    

    Or if I have misread the question, and you need a count of the orders if they order every month in the range, or 0 if they skipped a month then something like this (not tested so expect a typo or 2, would need the table def to test):-

    SELECT name, CASE  WHEN MonthCount = MonthOrderCount THEN OrderCount ELSE 0 END AS ContinuousOrderMonths
    FROM (
    SELECT CustName, COUNT(MonthName) AS MonthCount, SUM(MonthOrderCount) AS OrderCount, SUM(CASE WHEN MonthOrderCount > 0 THEN 1 ELSE 0 END)
    FROM (
    SELECT customer.name AS CustName, month.name AS MonthName, COUNT(order.id) AS MonthOrderCount
    FROM customer
    CROSS JOIN date
    LEFT OUTER JOIN order
    ON customer.id = order.customer_id
    AND MONTH(date.date) = MONTH(order.date)
    WHERE date.date BETWEEN startofdaterange AND endofdaterange
    GROUP BY customer.name, month.name )Sub1 ) Sub2
    GROUP BY CustName
    

    If you want a list of customers and a comma separated list of orders per month:-

    SELECT CustName, GROUP_CONCAT(CAST(MonthsOrder AS CHAR))
    FROM (
    SELECT customer.name AS CustName, month.name, COUNT(order.id) AS MonthsOrder
    FROM customer
    CROSS JOIN date
    LEFT OUTER JOIN order
    ON customer.id = order.customer_id
    AND MONTH(date.date) = MONTH(order.date)
    WHERE date.date BETWEEN startofdaterange AND endofdaterange
    GROUP BY customer.name, month.name) Sub1
    GROUP BY CustName
    

    You might have to expand this to get the month name with each one and force the order

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

Sidebar

Related Questions

I am having trouble writing query so that I can query the content of
I am having trouble writing a sql query and I was hoping that someone
I'm new to linq and I'm having trouble writing a query that pulls back
I'm having some trouble writing a query. For example: Let's say I have just
I'm having trouble writing this Query. I have 2 tables, vote_table and click_table. in
I'm quite new to sql and I'm having trouble writing this i have a
Im having some trouble writing a getstring function, this is what I have so
Ok, so, im having trouble with this program im writing, it all seems to
I am having trouble writing a query. I have been working with UNION on
I am having trouble writing a query and I don't even know if it

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.