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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help ... COUNT + GROUP BY

Author  Topic 

jpazcosta
Starting Member

5 Posts

Posted - 2008-05-29 : 18:58:00
hi everyone,

I have a table: Help

A B C
05/01/2008 100 1
05/01/2008 100 2
05/01/2008 100 2
05/02/2008 200 1
05/02/2008 200 2

SELECT a, COUNT(c) FROM Help GROUP BY a

Result:

1> 05/01/2008 3
2> 05/02/2008 2

But I need grouping columns B and C so that the result was

1> 05/01/2008 2
2> 05/02/2008 2

Is it possible? How can I do?
Thanks.

dshelton
Yak Posting Veteran

73 Posts

Posted - 2008-05-29 : 20:02:22
SELECT A, COUNT(DISTINCT C) FROM help
GROUP BY A
Go to Top of Page

jpazcosta
Starting Member

5 Posts

Posted - 2008-05-29 : 20:22:15
It works well with the example posted, thank you.

But if the table would be:

A B C
05/01/2008 100 1
05/01/2008 100 2
05/01/2008 100 2
05/01/2008 200 1
05/01/2008 200 2

Result:

1> 05/01/2008 2

I need:

1> 05/01/2008 4

Thanks !!!

quote:
Originally posted by dshelton

SELECT A, COUNT(DISTINCT C) FROM help
GROUP BY A

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-29 : 20:24:47
SELECT A, COUNT(*)
FROM YourTable
GROUP BY A, B, C

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

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

jpazcosta
Starting Member

5 Posts

Posted - 2008-05-29 : 20:41:03
If I could COUNT the result that returns this QUERY, work, but how?

Result:

1> 05/01/2008 1
2> 05/01/2008 2
3> 05/01/2008 1
4> 05/01/2008 1



quote:
Originally posted by tkizer

SELECT A, COUNT(*)
FROM YourTable
GROUP BY A, B, C

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

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx

Go to Top of Page

jpazcosta
Starting Member

5 Posts

Posted - 2008-05-30 : 19:46:44

SELECT A, COUNT(DISTINCT B + C)
FROM help
GROUP BY A

Thanks for the help !!!!!!!!

Go to Top of Page
   

- Advertisement -