在SQL中从字符串变量和时间变量(例如,term)创建新变量

时间:2017-09-12 17:08:38

标签: sql sql-server derived-column

我试图根据两个不同表中的两个其他变量派生变量。我想在他们的结束队列中知道一个学生的课程(即他们离开大学这个学期,无论毕业时间如何)。我认为合并可能是错误的功能。这是我的代码:

select distinct s.colleagueId,st.term, 
coalesce(EndProgram.EndProgram) EndProgram
from tbl_studentTerms st 
inner join dbo.tbl_Terms t
  on st.term = t.term
  inner join
   tbl_students s
   on st.colleagueId = s.colleagueId 

- 结束队列计划

left join (
select st.activeProgram as EndProgram
from tbl_studentTerms st
inner join tbl_students s
on s.colleagueId=st.colleagueId
where st.term = s.endingCohort) EndProgram
on EndProgram.colleagueId = s.colleagueId

1 个答案:

答案 0 :(得分:1)

您的End Cohort Program部分的最后一行,您正在加入“EndProgram.colleagueId = s.colleagueId” - 但是,您标记为EndProgram的select语句不会返回ColleagueID列,因此您无法加入吧。如果添加它,您可以解决该特定问题:

left join (
select st.activeProgram as EndProgram,
s.colleagueId as ColleagueID
from tbl_studentTerms st
inner join tbl_students s
on s.colleagueId=st.colleagueId
where st.term = s.endingCohort) EndProgram
on EndProgram.colleagueId = s.colleagueId