Pivoting in Sql server

Pivoting in Sql server

I have  a table (Labresult) with the followng data

sel	Testname				Regdate		value		units
0	ABSOLUTE EOSINOPHIL COUNT		10/06/2014	0.60		x 10³/µL
0	ALKALINE PHOSPHATASE (ALP)		10/06/2014	69.0		U/L
0	ALT (Alanine Transaminase)		10/06/2014	35.0		IU/L
0	Antibodies to Hepatitis C Virus 	10/06/2014	Non-Reactive	   
0	Antibodies to HIV 1 & 2 		10/06/2014	Negative	
0	AST (Aspartate Amino Transferase)	10/06/2014	29.0		IU/L
0	BASOPHIL				10/06/2014	0.5		%
0	BILIRUBIN    DIRECT			10/06/2014	0.1		mg/dL
0	BLOOD GLUCOSE (Fasting)		11/06/2014	115		mg/dL
0	BLOOD GLUCOSE (Fasting)		12/06/2014	120		mg/dL
0	BLOOD GLUCOSE (Fasting)		13/06/2014	163		mg/dL
0	BLOOD GLUCOSE (Fasting)		15/06/2014	93		mg/dL
0	BLOOD GLUCOSE (Fasting)		16/06/2014	103		mg/dL
0	CK-MB (Mass)			10/06/2014	1.0		ng/mL
0	Clostridium difficile Toxins - A & B 	13/06/2014	Negative	

Open in new window


I am pivoting it using the following qry

Declare @sql varchar(8000)

Set @sql = 'Select  Sel,Testname,Units'

Select @sql = @sql + ',(case when Regdate = '''+AD+''' then Value else '+''''''+'end) '+''''+AD+''''
from (Select distinct Regdate AD from Labresult1) q


Select @sql = @sql + ' from Labresult'


Exec(@sql) 

Open in new window


Sel	Testname			   Units		10/06/2014	11/06/2014	12/06/2014	13/06/2014	14/06/2014	15/06/2014	16/06/2014
0	ABSOLUTE EOSINOPHIL COUNT	x 10³/µL		0.60						
0	ALKALINE PHOSPHATASE (ALP)	   U/L		69.0						
0	ALT (Alanine Transaminase)	IU/L		35.0						
0	Antibodies to Hepatitis C Virus 	   	Non-Reactive						
0	Antibodies to HIV 1 & 2 			Negative						
0	AST (Aspartate Amino Transferase)  IU/L		29.0						
0	BASOPHIL	%				0.5						
0	BILIRUBIN    DIRECT		mg/dL		0.1						
0	BLOOD GLUCOSE (Fasting)	mg/dL						120				
0	BLOOD GLUCOSE (Fasting)	mg/dL								163			
0	BLOOD GLUCOSE (Fasting)	mg/dL												93	
0	BLOOD GLUCOSE (Fasting)	mg/dL															103
0	CK-MB (Mass)	ng/mL			1.0						
0	Clostridium difficile Toxins - A & B 								Negative			

Open in new window


This is ok except that i want to avoid duplicate names  under test names.

eg. Blood glucose(Fasting) appearing 5 times.

I want to appear it only once as below
						10/06/2014	11/06/2014	12/06/2014	13/06/2014	14/06/2014	15/06/2014	16/06/2014

0	BLOOD GLUCOSE (Fasting)	mg/dL						120		163				93		103		

Open in new window


I request expert advise on this.
venkataramanaiahsrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
please try this:
Declare @sql varchar(8000)

Set @sql = 'Select  Sel,Testname,Units'

Select @sql = @sql + ', max (case when Regdate = '''+AD+''' then Value else '+''''''+'end)) '+''''+AD+''''
from (Select distinct Regdate AD from Labresult1) q

Select @sql = @sql + ' from Labresult
group by  Sel,Testname,Units'

Exec(@sql)  

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alexandre SimõesManager / Technology SpecialistCommented:
Like this:
Declare @sql varchar(8000)

Set @sql = 'Select  Sel,Testname,Units'

Select @sql = 
  @sql + 
  ',MAX((case when Regdate = '''+AD+''' then Value else '+''''''+'end)) '+''''+AD+''''
  from (Select distinct Regdate AD from Labresult) q


Select @sql = @sql + ' from Labresult GROUP BY sel, Testname, units'

Exec(@sql) 

Open in new window


Just note that, apparently your value column is not numeric (one of the values is 'negative'.
You might want to consider changing this to a numeric type and use SUM instead of a MAX.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.