Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 count

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-02-07 : 23:51:45
I would like to count all into 1 column which is TotalCount

ID Status Subject
1 New CheckIn
2 New CheckIn
3 New Enquiry
4 Close CheckOut

My statement is not working.

select ID, Status, Subject, count(*) as totalCount

ID Status Subject TotalCount
1 New CheckIn 1
2 New CheckIn 1
3 New Enquiry 1
4 Close CheckOut 1

It suppose to be like this:

ID Status Subject TotalCount
1 New CheckIn 2
3 New Enquiry 1
4 Close CheckOut 1

Is it because the ID is different? How can I count including publishing the ID?
For my report, when I click totalcount 2, it will eleborate to more details. Is it possible? Is something like a link.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-02-08 : 00:04:59
select min(id) as id, status, subject, count(*) as totalCount
from yourTable
group by status, subject

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-02-08 : 01:12:05
But the ID show random ID
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-02-08 : 01:20:19
quote:
Originally posted by peace

But the ID show random ID



It is not random but the smallest ( min ) ID

Can you define for same status & Subject, which ID do you want ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-02-08 : 01:31:38
Im trying to apply for my report.
When I click totalCount 2, it will give the output as below:

ID Status Subject TotalCount
1 New CheckIn 1
2 New CheckIn 1
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-02-08 : 02:08:52
[code]
select ID, Status, Subject, TotalCount = 1
from yourTable
where Status = 'New'
and Subject = 'Checkin'
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -