Solved

Select Items from Inventory, Count the items

Posted on 2006-11-19
5
278 Views
Last Modified: 2009-07-29
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
Comment
Question by:sfink01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 17974886
try this

select items, sub, description, count(sub) 'NumItem'
from inventory
group by items, sub, description
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17974893
SELECT item, sub, description, COUNT(*) as itemCount
FROM inventory
GROUP BY item, sub, description
ORDER BY item, COUNT(*), sub
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 250 total points
ID: 17974907
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
 
LVL 29

Expert Comment

by:Nightman
ID: 17974943
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
 

Author Comment

by:sfink01
ID: 17974966
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question