1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

SQL Help (Max Function with Multiple Tables)

Discussion in 'BBS Hangout' started by Lil Pun, Aug 22, 2013.

Tags:
  1. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    I have two tables, Courses and Sections. The courses tables has date ranges on it so it may have a check that was good from 2006 - 2008 but had a new one implemented in 2009. I want to pull the max from that column for comparison between Courses check Sections check fields. See below.

    SELECT
    sections_year,sections_id,sections_sub,sections_numb,sections_seq,sections_check,courses_check

    FROM

    sections,courses

    WHERE

    sections_year = 2013 AND

    sections_sub = courses_sub AND

    sections_numb = courses_numb AND

    ssbsect_prereq_chk_method_cde != scbcrse_prereq_chk_method_cde

    courses_eff_year = (SELECT Max (courses_eff_year);


    Can anybody point me in the right direction? I know you can use a Max function without the GROUP BY but how can it be done? I tried to Google and find some solutions but haven't come up with much.
     
  2. superfob

    superfob Mommy WOW! I'm a Big Kid now.

    Joined:
    May 5, 2006
    Messages:
    2,091
    Likes Received:
    1,400
    courses_eff_year = (SELECT TOP 1 courses_eff_year FROM XXX ORDERED BY DESC YEAR)

    Wouldn't this work?
     
  3. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    Doesn't that just return the greatest record?
     
  4. superfob

    superfob Mommy WOW! I'm a Big Kid now.

    Joined:
    May 5, 2006
    Messages:
    2,091
    Likes Received:
    1,400
    Isn't that what you are trying to achieve with MAX?

    Not sure I understand what you are trying to do.
     
  5. Lil Pun

    Lil Pun Member

    Joined:
    Oct 6, 1999
    Messages:
    34,143
    Likes Received:
    1,038
    Here's a pic of what I am attempting to do:

    [​IMG]
     
  6. superfob

    superfob Mommy WOW! I'm a Big Kid now.

    Joined:
    May 5, 2006
    Messages:
    2,091
    Likes Received:
    1,400
    http://stackoverflow.com/questions/7745609/sql-select-only-rows-with-max-value-on-a-column

    So I guess to start off you need to find the highest eff_year for Courses table based on sub and numb.

    SELECT yt1.*
    FROM
    (SELECT *
    FROM [Courses]) yt1
    LEFT OUTER JOIN
    (SELECT *
    FROM [Courses]) yt2
    ON yt1.sub = yt2.sub AND yt1.numb = yt1.numb AND yt1.eff_year < yt2.eff_year
    WHERE yt2.sub is null

    Now you can join the Sections table to that.

    SELECT sec.*, cor.check
    FROM
    [Sections] sec
    JOIN
    **table above** called cor
    WHERE sec.year = 2013 AND sec.sub = cor.sub AND sec.numb = cor.numb AND sec.check != cor.check
     

Share This Page