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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:01:14+00:00 2026-05-10T22:01:14+00:00

I’ll be using the AdventureWorks Database to illustrate my problem. I need to show

  • 0

I’ll be using the AdventureWorks Database to illustrate my problem.

I need to show for a particular customer a list of OrderDate with the most Orders.

My intial attempt was as follows:

SELECT CustomerID, OrderDate, COUNT(1) Cnt FROM Sales.SalesOrderHeader WHERE CustomerID = 11300 GROUP BY CustomerID, OrderDate ORDER BY Cnt DESC 

This will get us the following result:

CustomerID    OrderDate             Cnt -----------   ----------                ---- 11300        2003-11-22 00:00:00.000     2 11300        2004-01-28 00:00:00.000     2 11300        2004-02-18 00:00:00.000     2 11300        2004-02-08 00:00:00.000     2 11300        2004-02-15 00:00:00.000     1 11300        2004-03-11 00:00:00.000     1 11300        2004-03-24 00:00:00.000     1 11300        2004-03-30 00:00:00.000     1 11300        2004-04-28 00:00:00.000     1 11300        2004-05-03 00:00:00.000     1 11300        2004-05-17 00:00:00.000     1 11300        2004-06-18 00:00:00.000     1 ... 

Not exactly what I wanted, as the result should only show all records where Cnt = 2, like so:

CustomerID    OrderDate             Cnt -----------   ----------                ---- 11300        2003-11-22 00:00:00.000     2 11300        2004-01-28 00:00:00.000     2 11300        2004-02-18 00:00:00.000     2 11300        2004-02-08 00:00:00.000     2 

I’m stuck because I can’t wrap my mind around two problems:

1) A customer might have more than one OrderDate with the same Cnt value. This means I can’t do something like TOP 1 to get the desired result.
2) Because the number of Orders for each customer may be different, I cannot use the following SQL statement:

SELECT CustomerID, OrderDate, COUNT(1) Cnt FROM Sales.SalesOrderHeader WHERE CustomerID = 11300 GROUP BY CustomerID, OrderDate HAVING COUNT(1) > 1 ORDER BY Cnt DESC 

This will work for getting the right result for this customer, but will definitely be wrong if the next customer has only one Order for a particular day.

So, either the query is impossible in this situation, or I am approaching the query in the wrong way. Any ideas on this problem is appreciated.

Also, since this will be a query in a stored procedure, any ideas on solving this in T-SQL will be acceptable.

UPDATE: Thanks to Mehrdad, I’ve been introduced to Common Table Expressions, and Life is Good®. 🙂

  • 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. 2026-05-10T22:01:14+00:00Added an answer on May 10, 2026 at 10:01 pm

    You should use TOP n WITH TIES clause to accomplish the task:

    SELECT TOP 1 WITH TIES CustomerID, OrderDate, COUNT(*) Cnt FROM Sales.SalesOrderHeader WHERE CustomerID = 11300 GROUP BY CustomerID, OrderDate ORDER BY Cnt DESC 

    Alternatively, you can use a common table expression (CTE) to solve the problem. This solution requires SQL Server 2005 or higher:

    WITH MyTable AS (SELECT CustomerID, OrderDate, COUNT(*) Cnt                  FROM Sales.SalesOrderHeader                  WHERE CustomerID = 11300                  GROUP BY CustomerID, OrderDate) SELECT CustomerID, OrderDate, Cnt FROM MyTable WHERE Cnt = (SELECT MAX(Cnt) FROM MyTable); 

    If you’re using the CTE method, make sure the previous statement is terminated by a semicolon (if it’s not the first statement). It’s required before a CTE expression.

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

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer INSERT INTO anothertable (list, of , column, names, to, give,… May 11, 2026 at 1:51 pm
  • added an answer I wouldn't restrict yourself to one filter per assembly. You… May 11, 2026 at 1:50 pm
  • added an answer Yes, you can access an array component directly. For example:… May 11, 2026 at 1:50 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.