Question : Multiple counts on 1 column

I have seen this post: http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_22528773.html?sfQueryTermInfo=1+1+10+30+column+count+multipl+sql

but I think my question is different because I want the end result to display the counts in different columns when I used the examples in the post above it displays the results in rows.

I tried the attached code syntax but it didnt work for me - err with prefixes


I am using SQL 2000

Example:
I have 2 tables table A and table B below

--------------------------------------
Table A

ClientID    ClientStartDate
1              1/1/2001
2              1/2/2001
3              3/5/2003
4              5/6/2002
5              4/7/2007

--------------------------------------
Table B

ClientID    AccountDate    ProductCode
1             2/2/2002           abc
1            5/6/2003            cde
1            6/6/2006            cde
2            1/2/2001            cde
3            3/5/2003           abc
3            5/8/2004           fgh
4            5/7/2003           abc
5            8/7/2007           abc


SQL Query:
need a count for each ProductCode

so the result should look something like this:
----------------------------------------------------------------------------------------------
ClientID        abc   cde    fgh             
----------------------------------------------------------------------------------------------
1                  1        2
2                           1
3                   1                1
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
Select T1.Column1 ,T1.c1 , T2.c2 from
(
select column1, count(*) as c1
from tbl_1
Where Column1 ='a'
Group By Column1
) T1
JOIN
(select column1, count(*) as c2
from tbl_1
Where Column1 ='B'
Group By Column1
) T2 ON T1.column1 = T2.Column1
Related Solutions: multiple counts in one column?

Answer : Multiple counts on 1 column

Something like this?
If you knew the product codes beforehand.

select A.ClientID,
 sum(case when B.ProductCode = 'abc' then 1 end) abc,
 sum(case when B.ProductCode = 'cde' then 1 end) cde,
 sum(case when B.ProductCode = 'fgh' then 1 end) fgh
from TableA A
left join tableB B on A.ClientID = B.ClientID
group by A.ClientID
Random Solutions  
 
programming4us programming4us