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

  • Home
  • SEARCH
  • 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 8741905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:24:08+00:00 2026-06-13T11:24:08+00:00

SQL appears to be more complex than I anticipated. My problem: for each customer,

  • 0

SQL appears to be more complex than I anticipated. My problem: for each customer, I would like to show the Customer ID and the total number of orders placed in 2011.

My table looks like this

Table: Order_t
Order_ID    Order_Date    Customer_ID
--------    ----------    -----------
1001        10/21/2011    1
1002        10/25/2011    8
1003        10/26/2011    15
1004        10/27/2011    5
1005        11/24/2011    3
1006        11/27/2011    2
1007        11/28/2011    11
1008        12/3/2011     12
1009        12/5/2011     1
1010         1/16/2012    4

I would like my query to display a table like this:

Customer_ID    Orders_Placed
-----------    -------------
1              2
2              1
3              1
5              1
8              1
11             1
12             1
15             1

My current query is this (I am currently completely neglecting the Date part because I haven’t even figured out the grouping yet:

SELECT Customer_ID, SUM(Order_ID) AS Orders_Placed
FROM Order_t
GROUP BY Order_ID, Customer_ID

And this is my obviously wrong query:

Customer_ID    Orders_Placed
-----------    -------------
1              1001
8              1002
15             1003
5              1004
3              1005
2              1006
11             1007
12             1008
1              1009
4              1010

Thanks for help, but I would also like to understand where the problem is in my logic. What crucial part do I seem to not understand?

  • 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-13T11:24:09+00:00Added an answer on June 13, 2026 at 11:24 am

    The problem with your logic is this

    GROUP BY Order_ID, Customer_ID
    

    Which means each combination of (Order_ID, Customer_ID) is put in a different GROUP. Since Order_ID alone is unique, practically no grouping is happening.

    To do it correctly, you need to GROUP BY the Customer_ID (it reads like what you need, doesn’t it), then COUNT the Orders. Finally, add the date filter as well.

    SELECT Customer_ID, COUNT(Order_ID) AS Orders_Placed
    FROM Order_t
    WHERE Order_Date >= #1/1/2011# and Order_Date < #1/1/2012#
    GROUP BY Customer_ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While using sql 2005, it appears I am unable to insert more than 8000
Hello everybody! I have a SQL (see above) and i would like to know
Question: Is there an easier /safer way of running more complex SQL queries in
Note: This issue appears to be limited to SQL Server 2005 SP2 I have
SQL Server provides the type [rowguid]. I like to use this as unique primary
Does anyone know of a way to create more complex shared schedules? If not,
So in Sql Server it appears that I cannot have specific steps within my
I am writing a large number of SQL scripts to be shared by a
I like the tight coupling between LinqToSql data context objects and the underlying SQL
Firefox appears to prefetch anything that is set in a meta tag like so:

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.