1

I have a table with the following data for sales and inventory (oh):

category sales  oh  item_num
Clothes  12     10  1
Clothes  11     10  1
Clothes  10     10  1
Clothes  5      10  1
Clothes  8      10  1
Clothes  4      10  1
Clothes  23     10  2
Clothes  5      10  2
Clothes  20     10  2
Clothes  5      10  2
Clothes  13     10  2
Clothes  9      10  2
Food     6      25  3
Food     8      25  3
Food     7      25  3
Food     14     25  3

I am trying to query this table to get a sum of both the sales and oh columns by category:

SELECT category, SUM(sales) AS sales, SUM(oh) AS oh
FROM data
GROUP BY category

However, the problem is I need the SUM(oh) to only sum distinct items but the SUM(sales) to sum all the values. So the result should be:

category   sales   oh
Clothes     125    20
Food        35     25

I tried SUM(DISTINCT oh), but that only works for distinct oh values not distinct items. I really need something like SUM(DISTINCT(item_num) oh).

I experimented with various window functions, but could not come up with a solution. Does anyone know how to return this kind of sum on a unique key?

2
  • 1
    I guess I'm a little puzzled still. Are you saying that oh values are dependent on item_num and thus have guaranteed fixed values for specific item_num values? If not, how would one choose? Commented Aug 8, 2018 at 15:32
  • Yes, each item_num has a single oh (# of items on hand in the store) at the end of the period, but each item_num will have several sales records during the period. For example item 1 might be a blue shirt, item 2 might be a red scarf, and item 3 might be a candy bar. The query should tell us how many total items were sold for each category and how many items we have in total for each category at the end of the period. Commented Aug 8, 2018 at 18:20

1 Answer 1

1

Here's how I'd do it:

SELECT category, SUM(sales) AS sales, SUM(oh) AS oh
FROM (
    SELECT category, SUM(sales) AS sales, oh
    FROM data
    GROUP BY category, item_num, oh
) ttl
GROUP BY category;

Basically tackle the problem in stages. First group up the items by category and item number to get the sum of sales then group and sum by category to get the sum of oh.

Result:

 category | sales | oh
----------+-------+----
 Food     |    35 | 25
 Clothes  |   125 | 20
(2 rows)

Edit: Included simplified query.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that works great. I also found it is possible to do this with just one subquery by using: SELECT category, SUM(sales) AS sales, SUM(oh) AS OH FROM (SELECT category, SUM(sales) AS sales, oh FROM data GROUP BY category, item_num, oh) AS ttl. I really appreciate your time in posting the solution.
Edited my answer to include the simplified query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.