Solved

SQL query - getting a list of counts from a single table

Posted on 2003-12-12
2
386 Views
Last Modified: 2010-05-18
Hey,

If i have a table 'cars' in my DB something like:

      id      type      color
      1      2      blue
      2      2      blue
      3      3      red
      4      6      red
      5      5      blue
      6      5      blue
      7      2      green

If i want to return the total number of entries per type,
 and also return the type in the same row.
Is this possible and how would i do it?

Select distinct type from cars.....will return a list of the distinct types

select count(*) from cars as Total.....will return the complete total of all cars

But can i get a result in the following format

      count      type
      3      2
      1      3
      2      5
      1      6
where count contains the total number of cars per type?


0
Comment
Question by:mcgettiu
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 125 total points
ID: 9927718
Hi mcgettiu,

Select Type,Count(*) From Cars Group By Type

Is all you need.

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 

Author Comment

by:mcgettiu
ID: 9927735
Thanks a mill
You got me out of a jam
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 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

14 Experts available now in Live!

Get 1:1 Help Now