这是我目前的询问:
select C.ACCOUNTNO,C.CONTACT,KEY1,KEY4 from contact1 C
left join CONTSUPP CS on C.accountno=CS.accountno
where
C.KEY1!='00PRSP'
AND (C.U_KEY2='2009 FALL'
OR C.U_KEY2='2010 SPRING'
OR C.U_KEY2='2010 J TERM'
OR C.U_KEY2='2010 SUMMER')
现在,我有另一个表(CONTSUPP),其中包含CONTACT1中每条记录的多个详细记录。例如 - 高中GPA,SAT,ACT等。我需要将这些值拉到与我的contact1结果相同的行中 - 但是根据列中的信息确定列标题。例如,我知道我可以这样做:
select C.ACCOUNTNO,C.CONTACT,KEY1,KEY4,STATE from contact1 C
left join CONTSUPP CS on C.accountno=CS.accountno
where
C.KEY1!='00PRSP'
AND (C.U_KEY2='2009 FALL'
OR C.U_KEY2='2010 SPRING'
OR C.U_KEY2='2010 J TERM'
OR C.U_KEY2='2010 SUMMER')
不要问为什么(请) - 但是州持有等级值。问题是,这给了我成绩而不知道什么类型的成绩,所以我需要做这样的事情(伪):
select C.ACCOUNTNO,C.CONTACT,C.KEY1,C.KEY4,
STATE as GPA when CS.CONTACT='High School'
STATE as SAT when CS.CONTACT='Test/SAT'
..
from contact1 C
left join CONTSUPP CS on C.accountno=CS.accountno
where
C.KEY1!='00PRSP'
AND (C.U_KEY2='2009 FALL'
OR C.U_KEY2='2010 SPRING'
OR C.U_KEY2='2010 J TERM'
OR C.U_KEY2='2010 SUMMER')
帮助?
答案 0 :(得分:3)
select
C.ACCOUNTNO,
C.CONTACT,
C.KEY1,
C.KEY4,
HichschoolCS.State as HighSchool,
TestSatCS.state as Test
from
contact1 C
left join CONTSUPP HichschoolCS on C.accountno=HichschoolCS.accountno
and HichschoolCS.contact = 'High School'
left join CONTSUPP TestSatCS on C.accountno=TestSatCS.accountno
and TestSatCS.contact = 'Test/SAT'
where
C.KEY1!='00PRSP'
AND (C.U_KEY2='2009 FALL'
OR C.U_KEY2='2010 SPRING'
OR C.U_KEY2='2010 J TERM'
OR C.U_KEY2='2010 SUMMER')
更新:添加了仅使用最高SAT分数的示例
select
C.ACCOUNTNO,
C.CONTACT,
C.KEY1,
C.KEY4,
HichschoolCS.State as HighSchool,
TestSatCS.state as Test
from
contact1 C
left join CONTSUPP HichschoolCS on C.accountno=HichschoolCS.accountno
and HichschoolCS.contact = 'High School'
left join (SELECT MAX(state) state,
accountno
FROM
CONTSUPP TestSatCS
WHERE
contact = 'Test/SAT'
GROUP
accountno) TestSatCS
on C.accountno=TestSatCS.accountno
where
C.KEY1!='00PRSP'
AND (C.U_KEY2='2009 FALL'
OR C.U_KEY2='2010 SPRING'
OR C.U_KEY2='2010 J TERM'
OR C.U_KEY2='2010 SUMMER')