开发者

"Invalid Identifier" when joining on a column I created in the SELECT Statement

Note: My research turned up this question, which provides a possible solution to my issue, but my question is more general: is that the kind of solution I should go for?

I would like to query an academic history database to give me a record for each pair of calculus classes a particular student has taken where one is the prerequisite of the other. If the database was set up nicely or the course numbering was reasonable, I could do:

SELECT ...
FROM Academic_History PrerequisiteCourse
JOIN Academic_History NextCourse 
     ON (NextCourse.CalculusLevel = PrerequisiteCourse.CalculusLevel + 1) 
WHERE ...

Of course the CalculusLevel field doesn't exist, so this is nonsense. Also, there are several course numbers that qualify as Calculus I, and several that qualify as Calculus II, and so on, and these change fairly often. That makes hardcoding all the prerequisite pairings into the JOIN statement like this a really bad idea:

SELECT ...
FROM Academic_History PrerequisiteCourse
JOIN Academic_History NextCourse 
     ON (NextCourse.CourseNumber = '231' AND PrerequisiteCourse.CourseNumber = '220'
      OR NextCourse.CourseNumber = '231' AND PrerequisiteCourse.CourseNumber = '221'
      OR NextCourse.CourseNumber = '241' AND PrerequisiteCourse.CourseNumber = '231'
      OR NextCourse.CourseNumber = '24-' AND PrerequisiteCourse.CourseNumber = '231'   
      ...)
WHERE ...

What I feel like I should do is create my "CalculusLevel" field on the fly, which would be much easier to maintain:

SELECT CASE PrerequisiteCourse.CRS_NBR
            WHEN '115' THEN '0'
            WHEN '220' THEN '1'
            WHEN '221' THEN '1' 
            ...
            END PrerequisiteCourseLevel,
       CASE NextCourse.CRS_NBR
            WHEN '115' THEN '0'
            WHEN '220' THEN '1'
            WHEN '221' THEN '1' 
            ...
            END NextCourseLevel,

F开发者_运维知识库ROM Academic_History PrerequisiteCourse
JOIN Academic_History NextCourse 
     ON (PrerequisiteCourseLevel + 1 = NextCourseLevel)
WHERE ...

But of course the join doesn't work, since those columns are not in those tables. Even if I move the condition out of the JOIN ON and into the WHERE clause, though, I get an "Invalid Identifier" error, presumably because these fields don't exist yet when the WHERE clause is being executed.

What's the right way to do this? I've come up with a couple solutions like the one I mentioned in the second code block, but they all feel like unprofessional hacks.

Thanks!


You could add reusable columns using a CTE:

;with   hist as
        (
        select  case ... end as NextCourseLevel
        ,       case ... end as PrerequisiteCourseLevel
        ,       *
        from    Academic_History
        )
select  *
from    hist t1
join    hist t2
on      t1.PrerequisiteCourseLevel + 1 = t2.NextCourseLevel

EDIT: Per your comment, you can refactor the with statement by expanding it everywhere it's used:

select  *
from    (
        select  case ... end as PrerequisiteCourseLevel
        ,       *
        from    Academic_History
        ) as t1
join    (
        select  case ... end as NextCourseLevel
        ,       *
        from    Academic_History
        ) as t2
on      t1.PrerequisiteCourseLevel + 1 = t2.NextCourseLevel
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜