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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:38:22+00:00 2026-05-30T20:38:22+00:00

I am trying to write a SQL statement that will create a flattened table

  • 0

I am trying to write a SQL statement that will create a flattened table from source table data. This is what I am trying to do :

  • get the status and sum of its quantity for a combination of orderid, partid

I tried to accomplish this with the following query based on case expression:

SELECT orderid, partid, 
       SUM(quantity) as total,
       status1 =  case [status] when 1 then SUM(quantity) else null end,
       status2 =  case [status] when 2 then SUM(quantity) else null end,
       status3 =  case [status] when 3 then SUM(quantity) else null end 
FROM   partsum
GROUP BY  orderid,
          partid,
          status;

But the results are not what I require. I know I am grouping with status but the query will not compile without adding it to the list.

enter image description here

  • 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-30T20:38:23+00:00Added an answer on May 30, 2026 at 8:38 pm

    There are a bunch of ways to write this. Also I converted your nulls to zeros, but it could be easy enough to switch them back to nulls if that’s what you really need. You didn’t specify whic version of SQL, so here are two solutions that will work:

    A SQL 2005+ Version:

    SELECT  DISTINCT 
            OrderID, 
            PartID,
            SUM(Quantity) OVER (PARTITION BY PartID) as Total,
            SUM( case [status] when 1 then quantity else 0 end) 
                    OVER (Partition By PartID) as Status1,
            SUM( case [status] when 2 then quantity else 0 end) 
                    OVER (Partition By PartID) as Status2,
            SUM( case [status] when 3 then quantity else 0 end) 
                    OVER (Partition By PartID)as Status3
    FROM    PartSum
    

    Should work on any version (and by any i mean at least back to SQL 6.5):

    SELECT  
            OrderID, 
            PartID,
            SUM(Quantity) as Total,
            SUM( case [status] when 1 then quantity else 0 end) as Status1,
            SUM( case [status] when 2 then quantity else 0 end) as Status2,
            SUM( case [status] when 3 then quantity else 0 end) as Status3
    FROM    PartSum
    GROUP BY  orderid,
              partid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a sql statement that will insert data given a few
I'm trying to write an insert statement for a SQL Server table that inserts
I'm trying to write sql that produces the desired result from the data below.
Im trying to write an SQL query that will check in the table ‘persons’
I am trying to write an sql trigger that will update another table whenever
I'm trying to write some SQL that will delete files of type '.7z' that
I'm trying to write a SQL script that inserts 2 rows into a table
I'm trying to write a stored procedure in SQL that will : Make a
I have a perplexing SQL select statement (ugly) that I'm trying to write in
I am trying to write a script that will copy all the data in

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.