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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:40:10+00:00 2026-05-18T19:40:10+00:00

I have a customer table and an orderdetail table the customer id is a

  • 0

I have a customer table and an orderdetail table
the customer id is a foriegn key in the orderdetail table (I’m simplifying here)

The orderdetail table contains the following columns
OrderId
ItemId
CustomerId
Size

The Size column can take on any one of the following values
1. lr
2. md
3. sm

So the orderdetail table could have the following records (I’ve comma delimited the columns)

OrderId ItemId CustomerId Size

1,1,30,lr
1,1,30,md
1,1,30,sm
2,1,30,lr
2,1,30,md
3,1,30,lr
3,1,30,sm
4,1,30,lr
5,1,30,md
6,2,30,sm
7,3,30,md
8,3,30,lr

What I’d like is a really efficient query (there’s millions of records in the order details tables) that has the following output for a given customerId (30 in this case)

ItemId  SizeLr  SizeMd  SizeSm
1         4       3        2
2         0       0        1
3         1       1        0

The query I’m using uses 3 group by queries (one each for “lr”, “md” and “sm”) and as a result it scans the table 3 times.

I’m looking for a solution that scans the table just onces. I think the solution is using the new Grouping Set feature in MSSQL 2008. But either ways, a solution using one scan if what I hope someone can help me with.

EDIT

The actual output needs to have other fields from the customer table and the order details table as well. These other fields are not dependent on the aggregates.

For example

ItemName  ItemId  SizeLr  SizeMd  SizeSm
   A         1      4       3       2
   B         2      0       0       1
   C         3      1       1       0
   Totals           5       4       4

It would nice if I could get the totals for each of the Size columns as well

  • 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-18T19:40:11+00:00Added an answer on May 18, 2026 at 7:40 pm

    How about this:

    SELECT ItemId, 
        SUM(CASE WHEN Size = 'Lr' THEN 1 ELSE 0 END) AS SizeLr,
        SUM(CASE WHEN Size = 'Md' THEN 1 ELSE 0 END) AS SizeMd,
        SUM(CASE WHEN Size = 'Sm' THEN 1 ELSE 0 END) AS SizeSm
    FROM OrderDetail 
    WHERE CustomerId = 30
    GROUP BY ItemId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a typical customer table; Id Int Identity Primary Key, FirstName varchar(255),
I have a Customer table and one of the fields here is for Status
I have a Customer table with a Primary key (int auto increment) and an
I have a Customer table --> CustomerNumber and CustomerName columns I have a Sales
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have an html table that is displaying customer orders. One of these columns
I have a customer table with two columns first_name and last_name . How can
I have a customer table in sqlserver which contains a rowversion field and I
Here is a simple example: Assume I have a customer table that has a
Please help me to generate the following query. Say I have customer table and

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.