Click here to Skip to main content
15,914,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..

This is Ramesh. This is My Sql Server 2008 Table

ClientID EmpName Code
1 A Visited
1 B Ptp
1 A Ptp
1 A Visited
1 B Visited
1 A Visited
1 B ptp

I want to How to retrieve the client wise I am passing only Client Id.

Output

EmpName Visited Ptp
A 3 1
B 1 2
Posted
Comments
hiren soni 30-Mar-12 1:23am    
can you make your question clear....It is difficult for me to understand.

Though PIVOT syntax is simple, I feel like newbies faces problem in understanding it. So adding to Abhinav's answer with the query you need.

SQL
--Temp table for holding the test value
DECLARE @ClientVisit TABLE ( ClientID INT, EmpName VARCHAR(50), Code VARCHAR(50))
INSERT INTO @ClientVisit VALUES (1, 'A', 'Visited')
INSERT INTO @ClientVisit VALUES (1, 'B', 'Ptp')
INSERT INTO @ClientVisit VALUES (1, 'A', 'Ptp')
INSERT INTO @ClientVisit VALUES (1, 'A', 'Visited')
INSERT INTO @ClientVisit VALUES (1, 'B', 'Visited')
INSERT INTO @ClientVisit VALUES (1, 'A', 'Visited')
INSERT INTO @ClientVisit VALUES (1, 'B', 'ptp')

--Your query
SELECT EmpName, [Visited], [ptp]
FROM
(SELECT EmpName, Code
FROM @ClientVisit) As Souce
PIVOT (COUNT(Code) FOR Code IN ([Visited], [ptp])) As p
 
Share this answer
 
Comments
ARK Kambhampati 30-Mar-12 3:37am    
Hi Stalin Thank you for replying.. Your query is very usefull and urgent for me.

Thanks a lot........
Using the pivot command[^] could help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900