0

Trying to do a Group By ITEM but I can't seem to get it to work, ORA-00904: "ITEM": invalid identifier. This is what I have tried so far:

SELECT sum(quantity) AS SUPPLY, TRIM((SELECT ITEM_ID FROM INVENTORY_ITEM WHERE INVENTORY_ITEM_KEY = INVENTORY_SUPPLY.INVENTORY_ITEM_KEY)) AS ITEM FROM INVENTORY_SUPPLY
WHERE SUPPLY_TYPE = 'ONHAND' AND SHIPNODE_KEY IN ('LDC', 'LDC-WEB', 'LDC-SOS') GROUP BY ITEM;

There can be multiple entries in the INVENTORY_SUPPLY table for each ITEM (matched by INVENTORY_ITEM_KEY) and I need to sum these quantities into one row.

3
  • 1
    Please provide sample data, desired results, and an explanation of what you are trying to do. A non-working query does not (necessarily) provide this information. Commented Aug 11, 2021 at 17:39
  • The INVENTORY_SUPPLY table contains entries for each INVENTORY_ITEM_KEY with an on hand quantity for various SHIPNODE_KEYS. I am filtering only the SHIPNODE_KEYS I want in the query above, but this leaves 3 entries for each INVENTORY_KEY since they have different SHIPNODE_KEYS. I am using the INVENTORY_KEY to get the corresponding ITEM from a different table as that is more human friendly for reading. But without being able to SUM the QUANTITY by ITEM, I see 3 entries in the table for each ITEM and I need to SUM these into 1 entry. Does that help Commented Aug 11, 2021 at 17:43
  • I believe @GordonLinoff was trying to get you to do this: stackoverflow.com/help/minimal-reproducible-example Commented Aug 11, 2021 at 18:14

1 Answer 1

2

According to my understanding, probably you will have to inner join the 2 tables instead of a scalar sub query, as group by executes first before the select clause. Hence it is not able to identify ITEM.

select sum(quantity) AS SUPPLY,  
ITEM_ID as ITEM
FROM INVENTORY_SUPPLY join INVENTORY_ITEM
on INVENTORY_ITEM.INVENTORY_ITEM_KEY = INVENTORY_SUPPLY.INVENTORY_ITEM_KEY
WHERE SUPPLY_TYPE = 'ONHAND' 
AND SHIPNODE_KEY IN ('LDC', 'LDC-WEB', 'LDC-SOS') 
GROUP BY INVENTORY_ITEM.ITEM_ID;
Sign up to request clarification or add additional context in comments.

2 Comments

That worked perfectly, sorry for not maybe explaining my question well enough, but you provided something that works for me!
Why wouldn't you accept the answer, then, @AWooster?

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.