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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:44:44+00:00 2026-05-17T22:44:44+00:00

I am trying to put together a query that gives me a report of

  • 0

I am trying to put together a query that gives me a report of number of sales of products and their stock level in 3 seperate locations (store branches)

The idea is to group together the results as follows:

    Prod.  | Loc1 Sales | Loc1 Stk | Loc2 Sales | Loc2 Stk | Loc3 Sales | Loc3 Stk 
    Item 1 | 323        | 34       | 23         | 7        |        119 | 54 
    Item 2 | 653        | 566      | 84         | 45       |        476 | 3434
    Item 3 | 121        | 23       | 300        | 5643     |        12  | 3434

I can start off by grabbing the products and the qty from one location:

SELECT    col.productname, sum(col.quantity) as qty
FROM      customerorderlines col
JOIN      customerorders co
ON        co.id = col.customerorder_id
WHERE     co.orderdate BETWEEN 'start date here' AND 'end date here'
AND       co.location_id = 1
GROUP BY  product_id
ORDER BY  qty DESC

Result:

productname | qty
item 1      | 146
item 2      | 74
item 3      | 63
item 4      | 49

Now I try to join the stock table on there:

SELECT     col.productname, sum(col.quantity) AS qty, sum(s.stocklevel) AS stocklevel
FROM       customerorderlines col
JOIN       customerorders co
ON         co.id = col.customerorder_id
JOIN       stock s 
ON         s.product_id = col.product_id
WHERE      co.orderdate BETWEEN 'start date here' AND 'end date here'
AND        co.location_id = 1
AND        s.location_id = 1
GROUP BY   col.product_id
ORDER BY   qty DESC

This, of course, doesnt work:

productname | qty | stocklevel
item 1      | 246 | 89123
item 2      | 98  | 18454
item 3      | 78  | 22565

Not only are the stocklevels wrong, it fluffs up the qty too.
Still, I know this isnt the write way of going about it, so I try with a join:

SELECT     col.productname, sum(col.quantity) AS qty, sl.stocklevel
FROM       customerorderlines col
JOIN       ( SELECT
             product_id
             ,location_id
             ,SUM(stocklevel) AS stocklevel
             FROM stock 
             GROUP BY product_id
           ) sl
ON         sl.product_id = col.product_id
JOIN       customerorders co
ON         co.id = col.customerorder_id
JOIN       stock s 
ON         s.product_id = col.product_id
WHERE      co.orderdate BETWEEN 'start date here' AND 'end date here'
AND        co.location_id = 1
AND        s.location_id = 1
GROUP BY   col.product_id
ORDER BY   qty DESC

With this join, which works as a query on it own, I wait about 8 minutes before I kill the process because it just sits there.

Any suggestions?

Thanks in advance,
Rob

  • 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-17T22:44:45+00:00Added an answer on May 17, 2026 at 10:44 pm
    SELECT  p.name,
            (
            SELECT  SUM(col.quantity)
            FROM    customerorder co
            JOIN    customerorderlines col
            ON      col.customerorder_id = co.id
            WHERE   co.localtion_id = 1
                    AND co.orderdate BETWEEN @start AND @end
                    AND col.product_id = p.id
            ),
            (
            SELECT  SUM(stocklevel)
            FROM    stock s
            WHERE   s.location_id = 1
                    AND s.product_id = p.id
            ),
            (
            SELECT  SUM(col.quantity)
            FROM    customerorder co
            JOIN    customerorderlines col
            ON      col.customerorder_id = co.id
            WHERE   co.localtion_id = 2
                    AND co.orderdate BETWEEN @start AND @end
                    AND col.product_id = p.id
            ),
            (
            SELECT  SUM(stocklevel)
            FROM    stock s
            WHERE   s.location_id = 2
                    AND s.product_id = p.id
            ),
            (
            SELECT  SUM(col.quantity)
            FROM    customerorder co
            JOIN    customerorderlines col
            ON      col.customerorder_id = co.id
            WHERE   co.localtion_id = 3
                    AND co.orderdate BETWEEN @start AND @end
                    AND col.product_id = p.id
            ),
            (
            SELECT  SUM(stocklevel)
            FROM    stock s
            WHERE   s.location_id = 3
                    AND s.product_id = p.id
            )
    FROM    product p
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to put together a MYSQL query that will count the number of
I am trying to put together a query that will display one specific record
I am trying to put together a mysql query, that links 3 tables together.
I'm trying to put together a query that will retrieve the statistics of a
I trying to put together an Android app that will take a picture and
I'm trying to put together a layout using Bootstrap that resembles your typical OS
I'm trying to put together a simple RSS widget (for my wordpress blog) that
Using PostgreSQL 8.4, I'm trying to put together the following query: SELECT (field_a +
I'm trying to put together a plugin script that takes a range of values
I'm trying to put together a SQL query to gather the top 10 looked-at

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.