Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
IngredientName NUTRIENTNAME NValue
MILK cow's Carbohydrates 4.4
MILK cow's Isoleucine 0.173
MILK cow's Alanine 0.107

i have the above table. i need to pivot it like the follwing.

IngredientName Carbohydrates Isoleucine Alanine
MILK cow's 4.4 0.173 0.107

how can i perform this operation?
Posted
Updated 31-Jan-22 3:37am

1 solution

SQL
select IngredientName ,Carbohydrates,Isoleucine , Alanine   from(select  IngredientName ,NUTRIENTNAME ,NValue from table1)
 up PIVOT (sum(Nvalue) for NUTRIENTNAME in(Carbohydrates,Isoleucine,Alanine)) as Pvt 
 
Share this answer
 
v4
Comments
kparun86 4-Feb-14 7:12am    
Nope it s not working. May i know why u have included sum (nvalue) in the query. actually i dont need to sum any n values.
King Fisher 4-Feb-14 7:19am    
if your table structure is this

IngredientName NUTRIENTNAME NValue
MILK cow's Carbohydrates 4.4
MILK cow's Isoleucine 0.173
MILK cow's Alanine 0.107


definetly ,it will work
King Fisher 4-Feb-14 7:20am    
you getting any error?
kparun86 5-Feb-14 0:18am    
Yep Now it s working. I Guess i made some mistake . now it s working fine thanks :)
King Fisher 5-Feb-14 0:22am    
Welcome

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