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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:39:19+00:00 2026-05-14T08:39:19+00:00

I have a stock table and I would like to create a report that

  • 0

I have a stock table and I would like to create a report that will show how often were items ordered.

“stock” table:

item_id |  pcs | operation
apples  |  100 | order
oranges |   50 | order
apples  | -100 | delivery
pears   |  100 | order
oranges |  -40 | delivery
apples  |   50 | order
apples  |   50 | delivery

Basically I need to join these two queries together.

A query which prints stock balances:

SELECT stock.item_id, Sum(stock.pcs) AS stock_balance
FROM stock
GROUP BY stock.item_id;

A query which prints sales statistics

SELECT stock.item_id, Sum(stock.pcs) AS pcs_ordered, Count(stock.item_id) AS number_of_orders
FROM stock
GROUP BY stock.item_id, stock.operation
HAVING stock.operation="order";

I think that some sort of JOIN would do the job but I have no idea how to glue queries together.

Desired output:

item_id | stock_balance | pcs_ordered | number_of_orders
apples  |             0 |         150 |                2
oranges |            10 |          50 |                1
pears   |           100 |         100 |                1

This is just example. Maybe, I will need to add more conditions because there is more columns. Is there a universal technique of combining multiple queries together?

  • 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-14T08:39:20+00:00Added an answer on May 14, 2026 at 8:39 am
    SELECT a.item_id, a.stock_balance, b.pcs_ordered, b.number_of_orders
    FROM
        (SELECT stock.item_id, Sum(stock.pcs) AS stock_balance 
        FROM stock 
        GROUP BY stock.item_id) a
    LEFT OUTER JOIN
        (SELECT stock.item_id, Sum(stock.pcs) AS pcs_ordered, 
                Count(stock.item_id) AS number_of_orders 
        FROM stock
        WHERE stock.operation = "order"
        GROUP BY stock.item_id) b
    ON a.item_id = b.item_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that I would like to be able to present ranked
I have 2 tables like this: Stock Table product_id bigint(20) qty float Sales Table
I would like to mention before continuing that I have looked at other questions
I have a table called Stock and another called Listed, within the Stock Table
I'm trying to write a simple stock check program, and I have a Table
I have a PHP-driven site that includes an XML stock feed, which is remotely
I have a table generated from some PHP code that lists a SMALL amount
Need a rope ;)) Take a look on example: I have a table: CREATE
I have the following tables; CREATE TABLE IF NOT EXISTS `tags` ( `tag_id` int(11)
I'm trying create a query that will output a total number, as well as

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.