• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

DISTINCT Query with totals

I have a table with a "Cat" field and a "SubCat" field.

I need a SQL query which returns the distinct SubCats for a given Cat and the total number of SubCats found for each distinct SubCats

My Table:

Cat                    Subcat          
--------              ----------
1                        1
1                        2
1                        3
1                        3


I want the query to return the distinct Subcats with totals for the given cat. Thus if my Cat was 1 as in the above example, I want the query to return:

Subcat           Total
--------           ---------
1                   1
2                   1
3                   2
0
Krapulator
Asked:
Krapulator
  • 2
1 Solution
 
nmcdermaidCommented:
something like this:


SELECT COUNT(*) As Total, SubCat
FROM Table
GROUP BY SubCat
0
 
nmcdermaidCommented:
After reading the rest of your post


SELECT COUNT(*) As Total, SubCat
FROM Table
WHERE SubCat = 1
GROUP BY SubCat


0
 
KrapulatorAuthor Commented:
Perfect. Thanks!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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