DISTINCT Query with totals

Posted on 2005-04-20
Last Modified: 2010-03-19
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
Question by:Krapulator
    LVL 30

    Expert Comment

    something like this:

    SELECT COUNT(*) As Total, SubCat
    FROM Table
    GROUP BY SubCat
    LVL 30

    Accepted Solution

    After reading the rest of your post

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

    LVL 1

    Author Comment

    Perfect. Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now