Solved

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

Posted on 2003-12-12
2
381 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
Comment Utility
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
Comment Utility
Thanks a mill
You got me out of a jam
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help on writing a script for check ndf file size 2 76
xBase++ 6 68
query returning everything 11 66
report returning null 21 49
In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
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!
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…

743 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