Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6072869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:12:27+00:00 2026-05-23T10:12:27+00:00

Note: My research turned up this question , which provides a possible solution to

  • 0

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,

FROM 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:12:28+00:00Added an answer on May 23, 2026 at 10:12 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've asked a similar question before, but I've done some more research and this
Note that I am not asking which to choose (MVC or MVP), but rather
NOTE: This question is only loosely related to tinyxml, however including details like that
(note: I'm usually more thorough in doing my research before asking a question here,
(This question has been rewritten from an issue with NSTextView following some further research)
Note this is not a duplicate of this similar but different question ! My
NOTE : In hindsight, I realize this may seem like a stupid question, but
Sorry for the simple question that I could research, but i crashed a database
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
Note : The code in this question is part of deSleeper if you want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.