Solved

Select Items from Inventory, Count the items

Posted on 2006-11-19
5
266 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
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

809 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