[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Pivoting in Sql server

Posted on 2014-07-16
2
Medium Priority
?
291 Views
Last Modified: 2014-07-31
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.
0
Comment
Question by:venkataramanaiahsr
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40201324
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
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40201328
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

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question