The problem is that you are attempting to both count
and display the ProductDescription
. If you want to display the ProductDescription
then there are a few different ways that you can do this.
Instead of applying the count
to the ProductDescription
, you can count
the PID
column:
SELECT *
FROM
(
SELECT CONVERT(CHAR(4), t2.Date, 100) + CONVERT(CHAR(4), t2.Date, 120) as RegistrationDate,
t1.ProductDesc as ProductDescription,
t1.PID
from table_2 t2
left outer join table_1 t1
on t1.PID = t2.PID
) tableT
PIVOT
(
count(PID) FOR RegistrationDate
IN ([Jan 2009],[Feb 2009],[Mar 2009],[Apr 2009],[May 2009],[Jun 2009])
) AS pvt;
See SQL Fiddle with Demo.
Or you can create a second column in the subquery to return the ProductDescription
twice. One column will be used in the count and the second will be used in the final display:
SELECT *
FROM
(
SELECT CONVERT(CHAR(4), t2.Date, 100) + CONVERT(CHAR(4), t2.Date, 120) as RegistrationDate,
t1.ProductDesc as ProductDescription,
t1.ProductDesc as Product
from table_2 t2
left outer join table_1 t1
on t1.PID = t2.PID
) tableT
PIVOT
(
count(ProductDescription) FOR RegistrationDate
IN ([Jan 2009],[Feb 2009],[Mar 2009],[Apr 2009],[May 2009],[Jun 2009])
) AS pvt;
See SQL Fiddle with Demo