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 2000 Forums
 Transact-SQL (2000)
 count occurances in field

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2007-02-23 : 09:34:32
Ok, trying to slowly piece together a complicated sql and here's one piece of the puzzle.

Let's say in tblA and in fldA I have the entries:

x
x
x
y
y
z
z
z

I want to count the number of x's, y's, and z's and return all that info in one sql statement.

But, later, there may be added in the field other letters, so it needs to be dynamic and return something like:

x 3
y 2
z 3

Counting one item is easy, but never thought about counting all distint items.

Thanks,

Zath

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-23 : 09:37:28
How difficult is this?

Select fldA, count(fldA) as [count]
From TblA
Group by fldA


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2007-02-23 : 09:45:53
Knew it had to be something simple!

Thanks,

Zath
Go to Top of Page
   

- Advertisement -