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 get row data as coulmn using Pivot
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 01/16/2013 :  09:02:57  Show Profile  Reply with Quote
hello all,

i have table in which i need to get data year wise like 2009,2010,2011,2012

i written this query

SELECT UserCN,LastName,FirstName,Gender,DOB,TotalPointsCoeff,Level FROM EXCZ_WellcentPointTotal ew
join USR_Demographics ud ON ew.UserID=ud.UserID
WHERE ew.UserID IN
(
SELECT DISTINCT ud.UserID FROM USR_Demographics ud
join EXCZ_measurements lu on ud.UserID=lu.UserID
WHERE ud.CustId=277 AND year(entryDate)=2009

INTERSECT

SELECT DISTINCT ud.UserID FROM USR_Demographics ud
join EXCZ_measurements lu on ud.UserID=lu.UserID
WHERE ud.CustId=277 AND year(entryDate)=2010

it is showing in column wise...

again i used pivot to get rquired result

select UserCN,LastName,FirstName,Gender,DOB,TotalPointsCoeff,Level from
(

select Year(entryDate) as pivot_col FROM EXCZ_WellcentPointTotal ew

INNER JOIN USR_Demographics as o ON (ew.UserID=o.UserID)
join EXCZ_measurements lu on o.UserID=lu.UserID
WHERE o.CustId=277
) as t
pivot
(
COUNT(entryDate for pivot_col in ([2009],[2010],[2011],[2012])
) as p

it not showing any result

how to do this one??

P.V.P.MOhan

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 01/16/2013 :  09:37:00  Show Profile  Reply with Quote

select Year(entryDate) as pivot_col, EntryDate FROM EXCZ_WellcentPointTotal ew 

INNER JOIN USR_Demographics as o ON (ew.UserID=o.UserID) 
join EXCZ_measurements lu on o.UserID=lu.UserID
WHERE o.CustId=277 
) as t 
pivot 
( 
COUNT(entryDate) for pivot_col in ([2009],[2010],[2011],[2012]) 
) as p


--
Chandu
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22755 Posts

Posted - 01/17/2013 :  05:02:13  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
For unknown number of years, use this
http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
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.06 seconds. Powered By: Snitz Forums 2000