?
Solved

Select Items from Inventory, Count the items

Posted on 2006-11-19
5
Medium Priority
?
303 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 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

765 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