SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to et count using Pivot table ????
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 02/21/2013 :  10:13:37  Show Profile  Reply with Quote
Hello all,

i have query where for Customer id i will show counts...

Select B.Cust_ID,B.ProfileOwnerEmpID,LAc.ProfileStatusID
 from Emp_Details E join Cust_BasicInfo B on E.EmpID=B.ProfileOwnerEmpID join Cust_Login LAc on LAc.Cust_ID=B.Cust_ID
where E.EmpID=2 
[CODE]
Cust_ID	ProfileOwnerEmpID	ProfileStatusID
62	2	56
64	2	57
31	2	54
22	2	54
32	2	55
16	2	54
40	2	56


but i need to get out put like this

one :
[CODE]
Profile 56==ProfileStatusID 57==ProfileStatusID 54==ProfileStatusID
2 count 2 count 2 count 2

James K
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 02/21/2013 :  12:40:20  Show Profile  Reply with Quote
SELECT
	*
FROM
(
Select B.Cust_ID,LAc.ProfileStatusID
 from Emp_Details E join Cust_BasicInfo B on E.EmpID=B.ProfileOwnerEmpID join Cust_Login LAc on LAc.Cust_ID=B.Cust_ID
where E.EmpID=2
) s
PIVOT (COUNT(Cust_ID) FOR ProfileStatusID IN ([56],[57],[54]))P
Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 02/22/2013 :  01:19:10  Show Profile  Reply with Quote
How to give alias names for [56],[57],[54] this column headers.they are coming after using pivot...

need to come like this
56 as firstcount,57 as Second Count,54 as third count

P.V.P.MOhan
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 02/22/2013 :  02:24:44  Show Profile  Reply with Quote
SELECT
Cust_ID, ProfileStatusID, [56] AS firstCount, [57] AS secondCount, [54] AS thirdCount
FROM
(
Select B.Cust_ID,LAc.ProfileStatusID
from Emp_Details E join Cust_BasicInfo B on E.EmpID=B.ProfileOwnerEmpID join Cust_Login LAc on LAc.Cust_ID=B.Cust_ID
where E.EmpID=2
) s
PIVOT (COUNT(Cust_ID) FOR ProfileStatusID IN ([56],[57],[54]))P


--
Chandu
Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 02/22/2013 :  02:35:43  Show Profile  Reply with Quote
while executing this query i am getting error as 'The multi-part identifier'

B.Cust_ID, LAc.ProfileStatusID i gave like this entire query baSED ON joins.so it is giving error how to over come this if i remove both in select query result coming fine.

ProfileOwnerEmpID
ProfileOwnerEmpID	56	57	54
2	2	1	3


[CODE]
firstCount secondCount thirdCount
2 1 3
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 02/22/2013 :  02:42:10  Show Profile  Reply with Quote

SELECT
ProfileOwnerEmpID, [56] AS firstCount, [57] AS secondCount, [54] AS thirdCount
FROM
(
Select B.Cust_ID,LAc.ProfileStatusID, B.ProfileOwnerEmpID
from Emp_Details E join Cust_BasicInfo B on E.EmpID=B.ProfileOwnerEmpID join Cust_Login LAc on LAc.Cust_ID=B.Cust_ID
where E.EmpID=2
) s
PIVOT (COUNT(Cust_ID) FOR ProfileStatusID IN ([56],[57],[54]))P


EDIT: Red Marked
--
Chandu

Edited by - bandi on 02/22/2013 03:23:45
Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 02/22/2013 :  02:48:25  Show Profile  Reply with Quote
getting the same error even if i give ProfileOwnerEmpID,Cust_ID if i remove the those columns getting query executed....[56] AS firstCount, [57] AS secondCount, [54] AS thirdCount these three are showing..... along with this three columns i need to show ProfileOwnerEmpID also....suggest me

P.V.P.MOhan
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 02/22/2013 :  03:35:03  Show Profile  Reply with Quote
Check my previous post.... I have edited

--
Chandu
Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 02/22/2013 :  04:00:16  Show Profile  Reply with Quote
thanks chandu....why i missed this one????.....

P.V.P.MOhan
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 02/22/2013 :  04:17:40  Show Profile  Reply with Quote
quote:
Originally posted by mohan123

thanks chandu....why i missed this one????.....
P.V.P.MOhan

Welcome
While pivoting you can select the columns that are in inner query....

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000