Count Records within a group

I have data in a table in the following format:

      Title     Status           Date
     car1       working        1-1-04    
     car1       working        1-2-04
     car1       working        1-3-04
     car 2      working        1-3-04
     car 4      in progress   1-4-04
     car 5      not working  1-5-04

I would like to be able write an SQL statement that first groups all records first by their title, then returns the count of the status...I normally would build a query to group the data by car, then in a separate query count the records in the first query grouped by status.  In this case, I want to do it all in 1 statement.
So output would tell me total # status' for these cars:  Specifically,  Working= 2     In progress= 1     not working = 1


                                                 

BSDTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lilian-arnaudCommented:
try :

Select Status,count(distinct Title)
  From table
group by Status
0
peter57rCommented:
Hello BSDT,

SELECT Count([status='working') AS WKnt, Count([Status]='in progress') AS PKnt, Count([Status]='not working') AS NKnt FROM Mytablename;


Pete
0
vandalesmCommented:

select title, status, count(status) as status_count from tableName group by title, status

hope this helps
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

tomgalloCommented:
Try this:

SELECT
 COUNT(Cars) as Total,
 Status
FROM
 Table
WHERE
  Status = 'working'
GROUP BY Status
UNION
SELECT
 COUNT(Cars) as Total,
 Status
FROM
 Table
WHERE
  Status = 'in progress'
GROUP BY Status
UNION
SELECT
 COUNT(Cars) as Total,
 Status
FROM
 Table
WHERE
  Status = 'not working'
GROUP BY Status

Hope that helps,
-tom
0
tomgalloCommented:
Sorry, you have to add DISTINCT (i forget):

SELECT DISTINCT COUNT(Title) as Total, Status FROM Table WHERE Status = 'working' GROUP BY Status
UNION
SELECT DISTINCT COUNT(Title) as Total, Status FROM Table WHERE Status = 'in progress' GROUP BY Status
UNION
SELECT DISTINCT COUNT(Cars) as Total, Status FROM Table WHERE Status = 'not working' GROUP BY Status
0
tomgalloCommented:
Hehe, I tried and again is not working... this will work:

SELECT COUNT(*) as Working FROM Table WHERE Status = 'working' GROUP BY Title
UNION
SELECT COUNT(*) as InProgress FROM Table WHERE Status = 'in progress' GROUP BY Title
UNION
SELECT COUNT(*) as NotWorking FROM Table WHERE Status = 'not working' GROUP BY Title

Sorry! This works!
-tom
0
HilaireCommented:
Since you just need the most recent status, you need to join a subquery as follows
Can you give it a try ?

select sum(case when a.status = 'working' then 1 else 0 end) as Working,
sum(case when a.status = 'in progress' then 1 else 0 end) as InProgress,
sum(case when a.status = 'not working' then 1 else 0 end) as NotWorking
from <YourTable> a
inner join (select title, max(date) as maxdate from <YourTable> group by title) b on a.Title = b.Title and a.date = b.maxdate

0
Wayne_OwenCommented:
You Could use a Crosstab Query as below,


TRANSFORM Count(Date) AS CountOfDate
SELECT Title
FROM Table1
GROUP BY Title
PIVOT Status;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.