SQL to retrieve min value of the combination

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ravindra Harve

    SQL to retrieve min value of the combination

    Hi,

    I have the following table

    1001 2 3
    1001 3 5
    1001 5 7
    1002 5 4
    1002 6 2

    I need to get a result

    1001 2 3
    1002 6 2

    How is it possible ?
  • Mark C. Stock

    #2
    Re: SQL to retrieve min value of the combination

    question's a little vague, but assuming you are trying to get records for
    the minimum values of the first and third column, for all rows in a table,
    you could try a subquery like

    select *
    from x
    where (col1, col3) in (
    select col1, min(col3)
    from x
    group by col1
    )

    but don't try this until you understand why it works ;-)

    -- mcs


    "Ravindra Harve" <rharve@hotmail .comwrote in message
    news:835dd0ad.0 401070930.575a2 5ba@posting.goo gle.com...
    | Hi,
    |
    | I have the following table
    |
    | 1001 2 3
    | 1001 3 5
    | 1001 5 7
    | 1002 5 4
    | 1002 6 2
    |
    | I need to get a result
    |
    | 1001 2 3
    | 1002 6 2
    |
    | How is it possible ?


    Comment

    • Julia Sats

      #3
      Re: SQL to retrieve min value of the combination

      Use this one , the same but easier
      select *
      from (
      select col1, min(col3)
      from x
      group by col1
      )

      "Mark C. Stock" <mcstockX@Xenqu ery .comwrote in message
      news:D_6dneS_Lc PC0WGiRVn-jg@comcast.com. ..
      question's a little vague, but assuming you are trying to get records for
      the minimum values of the first and third column, for all rows in a table,
      you could try a subquery like
      >
      select *
      from x
      where (col1, col3) in (
      select col1, min(col3)
      from x
      group by col1
      )
      >
      but don't try this until you understand why it works ;-)
      >
      -- mcs
      >
      >
      "Ravindra Harve" <rharve@hotmail .comwrote in message
      news:835dd0ad.0 401070930.575a2 5ba@posting.goo gle.com...
      | Hi,
      |
      | I have the following table
      |
      | 1001 2 3
      | 1001 3 5
      | 1001 5 7
      | 1002 5 4
      | 1002 6 2
      |
      | I need to get a result
      |
      | 1001 2 3
      | 1002 6 2
      |
      | How is it possible ?
      >
      >

      Comment

      • Mark C. Stock

        #4
        Re: SQL to retrieve min value of the combination

        Julia,

        Not the same by any means.

        Your select * does absolutely nothing -- the subquery results are simply
        passed thru unchanged

        Your subquery contains only two columns -- the assumption is that all
        columns should be displayed

        Try them both on the EMP table with the empno, deptno, and sal in the
        resultset and dept and sal in the subquery ("Write a report that lists the
        lowest paid employees in each department")

        -- mcs

        "Julia Sats" <julia.sats@sym patico.cawrote in message
        news:iKnLb.7982 2$BA6.1688941@n ews20.bellgloba l.com...
        | Use this one , the same but easier
        | select *
        | from (
        | select col1, min(col3)
        | from x
        | group by col1
        | )
        |
        | "Mark C. Stock" <mcstockX@Xenqu ery .comwrote in message
        | news:D_6dneS_Lc PC0WGiRVn-jg@comcast.com. ..
        | question's a little vague, but assuming you are trying to get records
        for
        | the minimum values of the first and third column, for all rows in a
        table,
        | you could try a subquery like
        | >
        | select *
        | from x
        | where (col1, col3) in (
        | select col1, min(col3)
        | from x
        | group by col1
        | )
        | >
        | but don't try this until you understand why it works ;-)
        | >
        | -- mcs
        | >
        | >
        | "Ravindra Harve" <rharve@hotmail .comwrote in message
        | news:835dd0ad.0 401070930.575a2 5ba@posting.goo gle.com...
        | | Hi,
        | |
        | | I have the following table
        | |
        | | 1001 2 3
        | | 1001 3 5
        | | 1001 5 7
        | | 1002 5 4
        | | 1002 6 2
        | |
        | | I need to get a result
        | |
        | | 1001 2 3
        | | 1002 6 2
        | |
        | | How is it possible ?
        | >
        | >
        |
        |


        Comment

        Working...