SQL Selecting multiple columns based on max value in one column

后端 未结 6 1674
天涯浪人
天涯浪人 2021-02-07 12:22

OK so I have looked theough the other solutions an no help. So here is what I am trying to do. I need to select the row with multiple columns where the value in one column is th

6条回答
  •  走了就别回头了
    2021-02-07 12:47

    You need to find your MAX values in a subquery, then use those results to join to your main table to retrieve the columns.

    SELECT t.OrderFileId, t.ItemNumber, t.ItemCost, t.Warehouse
        FROM YourTable t
            INNER JOIN (SELECT ItemNumber, MAX(OrderFileId) AS MaxOrderId
                            FROM YourTable
                            GROUP BY ItemNumber) q
                ON t.ItemNumber = q.ItemNumber
                    AND t.OrderFileId = q.MaxOrderId
    

提交回复
热议问题