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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:20:06+00:00 2026-05-19T02:20:06+00:00

I have a table structure similar to this: id | order1_id | order1_type |

  • 0

I have a table structure similar to this:

    id | order1_id | order1_type | order1_amount | order2_id | order2_type | order2_amount
------------------------------------------------------------------------------------------
    1        1           3             4                 1         4              5
    2        2           1             1                 1         3              2
    3        1           4             4                 2         2              1

I want to get the data like this:

order_id | order_type | order_amount

1          3            6
1          4            9
2          1            1
2          2            1

I want to group by type, and sum the order amounts. How can I do that ?

Thanks,

  • 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-19T02:20:07+00:00Added an answer on May 19, 2026 at 2:20 am

    I’m going to use a union in a subquery to line the columns up, then group and sum on that.

    Assuming you’re stuck with this less-than-ideal table structure, you may want to create a view that represents the subquery below, then run the group by/sum against that view. I’m guessing such a view might be useful in more places than just this one query.

    select t.order_id, t.order_type, sum(t.order_amount)
    from (select order1_id as order_id, order1_type as order_type, order1_amount as order_amount
          from orders
          union all
          select order2_id as order_id, order2_type as order_type, order2_amount as order_amount
          from orders  
          union all        
          select order3_id as order_id, order3_type as order_type, order3_amount as order_amount
          from orders 
          union all         
          select order4_id as order_id, order4_type as order_type, order4_amount as order_amount
          from orders    
          union all      
          select order5_id as order_id, order5_type as order_type, order5_amount as order_amount
          from orders) t
    group by t.order_id, t.order_type
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure similar to the following picture. Table A has_many Table
I have a table structure that looks like: <table> <tr id=row1> <td> <div>row 1
I have a table with a structure like the following: LocationID AccountNumber long-guid-here 12345
This is the MySQL table structure that I have: itemID (BIGINT) userID (BIGINT) wasAdded
I have already googled for this I have a Table with following structure in
I have a table structure similar to: Portfolios Properties Units Leases All tables are
I have a MySQL server with many databases that have a similar table structure.
I have this table structure: CREATE TABLE users ( uid bigint NOT NULL, first_name
I have the following table structure with data as ReadingDate Unit 01-05-2011 10 01-06-2011
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party

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.