Question : sql -rows to columns

Hi ,

can you tell me the SQL for the below scenario...

i have a table "Test" in which ... Each Dr has performed some procedures  on certain date.
Three types of Procedures :-> 11,12,13
Now i want to see each doctor and count of each procedure type  he made

-------Values in the Table Test --------
ping--11--12--13--'1/1/2009'
ping      --11--NULL--13--'2/1/2009'
ping      --NULL---      12--13--'4/1/2009'
ping--11--12--13--'4/6/2009'
ping--NULL--12--13--'4/11/2009'
ming-11--12--13--'2/1/2009'
ming      --11--NULL--13--'3/2/2009'
ming--NULL--12--13--'3/5/2009'
ming--11--12--NULL--'5/1/2009'


Expected Result:-
Doctor -- Procedure# --Count
     Ping  ----------11 ----------  3
     ping ----------12 -------------- 4
   ...
         Ming ------- 11------------ --4
          Ming  -------- 12-------- -- 3

in SQL 2008

Answer : sql -rows to columns

Try this instead..ignore this previous post
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
CREATE TABLE #T
(
Name Varchar(10),
P1No Int,
P2No Int,
P3No Int,
EntryDate DateTime
)


INSERT INTO #T  VALUES('Ping',11,12,13,'1/1/2009')
INSERT INTO #T  VALUES('Ping',11,NULL,13,'1/1/2009')
INSERT INTO #T  VALUES('Ping',NULL,NULL,NULL,'1/1/2009')
INSERT INTO #T  VALUES('Ming',11,12,13,'1/1/2009')
INSERT INTO #T  VALUES('Ming',11,12,13,'1/1/2009')
INSERT INTO #T  VALUES('Ming',NULL,12,13,'1/1/2009')


SELECT Name,ProcedureNo,SUM(Count_no) As Total FROM
(
SELECT Name,p1No  as ProcedureNo,COUNT(*) as Count_no FROM #T
GROUP BY Name,p1No
UNION
SELECT Name,p2No as ProcedureNo,COUNT(*) as Count_no FROM #T
GROUP BY Name,p2No
UNION
SELECT Name,p3No as ProcedureNo,COUNT(*) as Count_no FROM #T
GROUP BY Name,p3No
)A
WHERE ProcedureNo IS NOT NULL
Group by Name,ProcedureNo
ORDER BY Name,ProcedureNo
Random Solutions  
 
programming4us programming4us