Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2009
    Posts
    12

    Unanswered: Try to figure this one out? Getting one column value based on order of another.

    Hi,
    I'm trying to build a query that will pull a single primary key id based on the minimum value of another column which is grouped by a foreign key.

    For Ezxample:
    Code:
    Column 1 | Column 2 | Column 3
    (PK)         (FK)         (Year)
    -------------------------------
    1111         101           2008
    1222         101           2007
    1333         101           2010
    I want to get the PK id of 1222 because it's the smallest Year of FK 101.

    My initial query was to select the smallest PK, but that was assuming that the PKs were created in order by year. However 2007 could be created after 2008.

  2. #2
    Join Date
    Nov 2003
    Posts
    2,933
    Provided Answers: 12
    Code:
    SELECT *
    FROM the_table_with_no_name
    WHERE column_3 = (SELECT min(t2.column_3)
                      FROM the_table_with_no_name t2
                      WHERE t2.column_2 = 101)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •