第一个表是User_Nutrient_Amount(多列PK,它们都是FK:2ndID映射到第二个表PK,ID1映射到第三个表PK)
UserID| NutID | NutValue
1 | 1 | 3.3
1 | 2 | 9.8
1 | 3 | 10.5
. | . | ...
第二张表是营养素(ID是PK)
NutID| Name
1 | Sodium
2 | Potassium
3 | Fat
. | .
第3个表是用户(ID是PK)
UserID| Name
1 | Joe
我想获得这样的数据
Sodium | Potassium | Fat | .
3.3 | 9.8 | 10.5 | Value.
其中结果都属于第3表中的ID(结果应该是一行)。
我尝试过一些连接并设法获得所需的结果,但随后在第3个表中添加了另一个ID并返回了多行(外连接)。
编辑(编辑上面的数据):
这是我的原始查询:
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
where 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
答案 0 :(得分:0)
您可以尝试此PIVOT查询:
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;
基本上,您需要在PIVOT子句和主SELECT子句中列出所有可能的(和必要的)营养素符号。