Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL: Selecting Column Based on MAX(Other Column)

I'm hoping there's a simple way to do this without using a sub-query:

Scenario: You have "TableA" with columns "Key", "SubKey", and "Value". I need to get the "Value" of the MAX("SubKey") for a given "Key".

So if the Table contained the rows:

KEY SUBKEY VALUE 1   1      100 1   2      200 1   3      300 

For Key = 1, I need the value 300. I was hoping to do something like this:

SELECT   VALUE FROM   TableA WHERE   Key = 1 HAVING   SubKey = MAX(SubKey) 

But that's a no-go. Is there a way to do this without doing a 'WHERE SubKey = (subselect for max subkey)'?

like image 675
John Avatar asked Sep 09 '10 20:09

John


People also ask

How do I get the maximum value from another column of a table in SQL?

In SQL Server there are several ways to get the MIN or MAX of multiple columns including methods using UNPIVOT, UNION, CASE, etc… However, the simplest method is by using FROM … VALUES i.e. table value constructor. Let's see an example. In this example, there is a table for items with five columns for prices.

How do I select multiple columns based on condition in SQL?

When we have to select multiple columns along with some condition, we put a WHERE clause and write our condition inside that clause. It is not mandatory to choose the WHERE clause there can be multiple options to put conditions depending on the query asked but most conditions are satisfied with the WHERE clause.

Can we use MAX function in where clause?

Overview. The MAX() function is used with the WHERE clause to gain further insights from our data. In SQL, the MAX() function computes the highest or maximum value of numeric values in a column.


1 Answers

Using a self join:

This will return all the values with subkey values that match, in case there are multiples.

SELECT a.value   FROM TABLE a   JOIN (SELECT MAX(t.subkey) AS max_subkey           FROM TABLE t          WHERE t.key = 1) b ON b.max_subkey = a.subkey  WHERE a.key = 1 

Using RANK & CTE (SQL Server 2005+):

This will return all the values with subkey values that match, in case there are multiples.

WITH summary AS (   SELECT t.*,          RANK() OVER(ORDER BY t.subkey DESC) AS rank     FROM TABLE t    WHERE t.key = 1) SELECT s.value   FROM summary s  WHERE s.rank = 1 

Using ROW_NUMBER & CTE (SQL Server 2005+):

This will return one row, even if there are more than one with the same subkey value...

WITH summary AS (   SELECT t.*,          ROW_NUMBER() OVER(ORDER BY t.subkey DESC) AS rank     FROM TABLE t    WHERE t.key = 1) SELECT s.value   FROM summary s  WHERE s.rank = 1 

Using TOP:

This will return one row, even if there are more than one with the same subkey value...

  SELECT TOP 1          t.value     FROM TABLE t    WHERE t.key = 1 ORDER BY t.subkey DESC 
like image 184
OMG Ponies Avatar answered Oct 13 '22 19:10

OMG Ponies