[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

Select Items from Inventory, Count the items

I need to select the items from the table inventory (items) and their sub catagory (sub) and description (descr) then count the number of items in each subcategory group them by subcategory and then order them by the number of items in each sub category.  Output needs to include item, sub, descr and number of items.

Thanks in advance.
0
sfink01
Asked:
sfink01
1 Solution
 
hongjunCommented:
try this

select items, sub, description, count(sub) 'NumItem'
from inventory
group by items, sub, description
0
 
NightmanCTOCommented:
SELECT item, sub, description, COUNT(*) as itemCount
FROM inventory
GROUP BY item, sub, description
ORDER BY item, COUNT(*), sub
0
 
Brian CroweCommented:
I don't believe the solution provided above will work since you are including the item in your grouping.

SELECT inventory.item, inventory.descr, category.sub, category.subcount
FROM inventory
INNER JOIN (SELECT sub, COUNT(*) AS SubCount FROM inventory GROUP BY sub) AS category
   ON inventory.sub = category.sub
ORDER BY category.subcount
0
 
NightmanCTOCommented:
It will work if the requirement is to count on the item,subcategory combination, which was my understanding.

If you need to show totals for the subcategory across ALL items, the BriCrowes solution would be better.
0
 
sfink01Author Commented:
This is what I ended up with from BriCrowe's answer I modified it but it pointed me in the right direction.

SELECT category.sub AS "Sub Category", inventory.item AS "Item No.",
 inventory.descr AS " Item Description",
 category.subcount as "No. Items in Sub Category"
FROM inventory
INNER JOIN (SELECT sub, COUNT(*) AS SubCount FROM inventory
GROUP BY sub) AS category ON inventory.sub = category.sub
ORDER BY category.subcount DESC

Thanks for the quick responses!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now