SQL statement that will take 70 or so rows from multiple tables and pivot them to columns
First Table is User_Nutrient_Amount (Multiple Column PK which are both FK: 2ndID maps to 2nd Table PK, ID1 maps to 3rd Table PK)
UserID| NutID | NutValue
1 | 1 | 3.3
1 | 2 | 9.8
1 | 3 | 10.5
. | . | ...
2nd Table is Nutrient (ID is PK)
NutID| Name
1 | Sodium
2 | Potassium
3 | Fat
. | .
3rd Table is Users (ID is PK)
UserID| Name
1 | Joe
I would like to get the data such that
Sodium | Potassium | Fat | .
3.3 | 9.8 | 10.5 | Value.
where the results all belong to on ID from the 3rd Table (result should be one row).
I've tried a few joins and managed to get the desired results, but then added another ID to the 3rd table and got back multiple rows (outer join).
Edit (edited the data above):
Here is my original query:
select ZN, CU, MN, SE, RT, BC, ATMG, [D-IU], VITC, THIA, RIBO, MG, PANT, B6, FOLA, B12, VITK, FOAC, CHOL, TRFA, TSAT, MUFA, PUFA, STAR, TOmega3, TOmega6, PROT, FAT, CARB, KCAL, TSUG, TDF, CA, FE, MG, P, K, NA from
(select User_Amount.NutrientValue as PROT, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'PROT' and User_Amount.UserID = 1) PROT left outer join
(select User_Amount.NutrientValue as FAT, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'FAT') FAT on FAT.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as CARB, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'CARB') CARB on CARB.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as KCAL, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'KCAL') KCAL on KCAL.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as TSUG, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'TSUG') TSUG on TSUG.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as TDF, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'TDF') TDF on TDF.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as CA, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'CA') CA on CA.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as FE, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'FE') FE on FE.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as MG, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'MG') MG on MG.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as P, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'P') P on P.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as K, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'K') K on K.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as NA, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'NA') NA on NA.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as ZN, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'ZN') ZN on ZN.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as CU, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'CU') CU on CU.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as MN, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'MN') MN on MN.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as SE, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'SE') SE on SE.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as [RT], User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
w开发者_运维技巧here Nutrient.NutrientSymbol = 'RT-æG') [RT] on [RT].UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as [BC], User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'BC-æG') [BC] on [BC].UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as ATMG, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'ATMG') ATMG on ATMG.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as [D-IU], User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'D-IU') [D-IU] on [D-IU].UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as VITC, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'VITC') VITC on VITC.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as THIA, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'THIA') THIA on THIA.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as RIBO, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'RIBO') RIBO on RIBO.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as [N-MG], User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'N-MG') [N-MG] on [N-MG].UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as PANT, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'PANT') PANT on PANT.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as B6, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'B6') B6 on B6.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as FOLA, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'FOLA') FOLA on FOLA.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as B12, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'B12') B12 on B12.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as VITK, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'VITK') VITK on VITK.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as FOAC, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'FOAC') FOAC on FOAC.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as CHOL, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'CHOL') CHOL on CHOL.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as TRFA, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'TRFA') TRFA on TRFA.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as TSAT, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'TSAT') TSAT on NA.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as MUFA, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'MUFA') MUFA on MUFA.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as PUFA, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'PUFA') PUFA on PUFA.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as STAR, User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'STAR') STAR on STAR.UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as [TOmega3], User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'TOmega n-3') [TOmega3] on [TOmega3].UserID = PROT.UserID left outer join
(select User_Amount.NutrientValue as [TOmega6], User_Amount.UserID
from User_Amount inner join Nutrient on Nutrient.NutrientID = User_Amount.NutrientID
where Nutrient.NutrientSymbol = 'TOmega n-6') [TOmega6] on [TOmega6].UserID = PROT.UserID
You could try this PIVOT query:
SELECT
ZN, CU, MN, SE,
[RT-æG] AS RT,
[BC-æG] AS BC,
ATMG, [D-IU], VITC,
THIA, RIBO, MG,
PANT, B6, FOLA,
B12, VITK, FOAC,
CHOL, TRFA, TSAT,
MUFA, PUFA, STAR,
TOmega3, TOmega6,
PROT, FAT, CARB,
KCAL, TSUG, TDF,
CA, FE, P, K, NA
FROM (
SELECT u.NutrientValue, n.NutrientSymbol
FROM User_Amount u INNER JOIN Nutrient n ON n.NutrientID = u.NutrientID
WHERE u.UserID = 1
) t
PIVOT (
MAX(NutrientValue) FOR NutrientSymbol IN (
ZN, CU, MN, SE,
[RT-æG], [BC-æG],
ATMG, [D-IU], VITC,
THIA, RIBO, MG,
PANT, B6, FOLA,
B12, VITK, FOAC,
CHOL, TRFA, TSAT,
MUFA, PUFA, STAR,
TOmega3, TOmega6,
PROT, FAT, CARB,
KCAL, TSUG, TDF,
CA, FE, P, K, NA)
) x;
Basically, you need to list all possible (and necessary) Nutrient symbols both in the PIVOT clause and in the main SELECT clause.
精彩评论