Groups | Blog | Home
all groups > sql server (alternate) > september 2004 >

sql server (alternate) : Newbie problem with counting rows



webermax2004 NO[at]SPAM yahoo.fr
9/6/2004 3:37:43 AM
This is a refinement of my previous problem. I have the following
table :

C0 | C1 | C2
----+----+----
A | 1 | X
----+----+----
A | 1 | X
----+----+----
A | 2 | X
----+----+----
A | 1 | Y
----+----+----
B | 1 | X
----+----+----
B | 1 | X

I want to write a request which counts the number of different (C0,
C1, C2) where C2 is X. Here, the result should be 3. What would be the
request ? A kind of :

select count(*) from (select distinct C0, C1 from T where C2 = 'X')

christian.maslen NO[at]SPAM techie.com
9/6/2004 3:27:49 PM
[quoted text, click to view]

You need to give your table expression a correlation name:

select count(*) from (select distinct C0, C1 from T where C2 = 'X') as t


David Portas
9/6/2004 8:57:48 PM
You almost had it :-)

select count(*) from (select distinct C0, C1 from T where C2 = 'X') AS X

An alias is mandatory for a derived table even if it isn't referenced
anywhere.

--
David Portas
SQL Server MVP
--

AddThis Social Bookmark Button