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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:35:39+00:00 2026-06-18T06:35:39+00:00

I have a large order list that I need to create, and I need

  • 0

I have a large order list that I need to create, and I need to get a list of product codes and their quantities on a per order basis.

To simplify, I’m showing an example for a single order, but the outer query will need to provide this result for every order in the system.

So far, I have the following:

mysql> select ProductCode,Quantity from OrderProducts where Order_ID = X;
+-------------+----------+
| ProductCode | Quantity |
+-------------+----------+
| PROD1       |        1 |
| PROD2       |        1 |
| PROD1       |        1 |
+-------------+----------+
3 rows in set (0.00 sec)

mysql> SELECT CONCAT(OrderProducts.ProductCode,' (',SUM(OrderProducts.Quantity),')') FROM OrderProducts WHERE Order_ID = X GROUP BY OrderProducts.ProductCode;
+------------------------------------------------------------------------+
| CONCAT(OrderProducts.ProductCode,' (',SUM(OrderProducts.Quantity),')') |
+------------------------------------------------------------------------+
| PROD1 (2)                                                              |
| PROD2 (1)                                                              |
+------------------------------------------------------------------------+
2 rows in set (0.00 sec)

What I need is a GROUP_CONCAT result of the query above,but it won’t let me wrap a GROUP_CONCAT around the CONCAT shown above because I think because the SUM() is already doing grouping itself. Here is what I need to get back:

PROD1 (2),PROD2 (1)

EDIT

I was unclear about the use of the query above. I will be using that query as a subquery within a select which will be querying on ALL orders in Orders. Below is an updated version of the query:

SELECT 
  ( 
    SELECT 
      GROUP_CONCAT(SerialNumberedProductCount) 
    FROM  
    ( 
      SELECT 
        OrderProducts.Order_ID,
        CONCAT(OrderProducts.ProductCode,' (',SUM(OrderProducts.Quantity),')') AS SerialNumberedProductCount 
       FROM  
        OrderProducts, Products 
       WHERE 
        OrderProducts.Order_ID = Orders.Order_ID AND 
        OrderProducts.Product_ID = Products.Product_ID AND 
        Products.IsSerialNumbered = 1 
       GROUP BY 
        OrderProducts.ProductCode
    ) SerialNumberedProductCountsSubQuery 
    WHERE 
      SerialNumberedProductCountsSubQuery.Order_ID = Orders.Order_ID
  ) AS SerialNumberedProductCounts 
FROM 
  Orders

With the above query, I am getting the following error:

ERROR 1054 (42S22): Unknown column 'Orders.Order_ID' in 'where clause'

  • 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-18T06:35:41+00:00Added an answer on June 18, 2026 at 6:35 am

    To apply another aggregate, you will need to wrap it in a subquery. This will apply the aggregate GROUP_CONCAT() over all rows returned by the subquery though, as is the output from your desired example. Since the inner subquery only returns the one already aggregated column, there is no need for a GROUP BY in the outer query

    SELECT GROUP_CONCAT(prod_sum)
    FROM (
      SELECT CONCAT(OrderProducts.ProductCode,' (',SUM(OrderProducts.Quantity),')') AS prod_sum 
      FROM OrderProducts 
      WHERE Order_ID = 'X'
      GROUP BY OrderProducts.ProductCode
    ) subq
    

    http://sqlfiddle.com/#!2/68bca/4

    Update after re-post

    To join the results of your subqueries against Orders, you probably ought to be joining this entire construct in instead of using a subselect.

    SELECT 
      /* Replace these with the columns you actually need */
      Orders.*,
      SerialNumberedProductCounts.*
    FROM 
      Orders
      LEFT JOIN ( 
        SELECT
          Order_ID, 
          GROUP_CONCAT(SerialNumberedProductCount) 
        FROM  
        ( 
          SELECT 
            OrderProducts.Order_ID,
            CONCAT(OrderProducts.ProductCode,' (',SUM(OrderProducts.Quantity),')') AS SerialNumberedProductCount 
           FROM  
            OrderProducts, Products 
           WHERE 
            OrderProducts.Product_ID = Products.Product_ID AND 
            Products.IsSerialNumbered = 1 
           GROUP BY 
            OrderProducts.ProductCode
        ) SerialNumberedProductCountsSubQuery 
        GROUP BY Order_Id
      ) AS SerialNumberedProductCounts 
      ON SerialNumberedProductCounts.Order_ID = Orders.Order_ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large array of numerical data that I need to sort, insert,
I have a need to optimize a large apps that use linq extensively. Many
I have list that contains dictionary that need to be sorted based on the
I have an list that toggles with no problem in FF. I need this
I have a large list of images that have been misnamed by my artist.
I have a large list of LDAP DN's that are all related in that
I have a rather large SQL file which starts with the byte order marker
i have large numbers of text files and i am in problem that i
I have large video files (~100GB) that are local on my machine. I have
I'm trying to create a faster query, right now i have large databases. My

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.