Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the minimum value between several columns

I'm using SQL Server 2008;

Suppose I have a table 'X' with columns 'Date1', 'Date2', 'Dateblah', all of type DateTime.

I want to select the min value between the three columns, for example (simplified, with date mm/dd/yyyy)

ID       Date1          Date2           Dateblah
0     09/29/2011      09/20/2011       09/01/2011 
1     01/01/2011      01/05/2011       03/03/2010

ID    MinDate
0    09/01/2011
1    03/03/2010

Is there a bread and butter command to do that ?

Thanks in advance.

EDIT: I've seen this question What's the best way to select the minimum value from several columns? but unfortunately it won't suit me as I'm being obligated to do it against normalization because I'm making tfs work item reports, and the 'brute-force' case thing will end up being a pain if I have 6 ou 7 columns.

like image 815
Conrad Clark Avatar asked Sep 29 '11 13:09

Conrad Clark


People also ask

How do I get the minimum value from multiple columns in SQL?

We can use a nested CASE statement to compare the values of multiple columns to get the minimum value. Also, we can use the nested IIF statement.

How do I get the maximum value from multiple columns in SQL?

If you're working with MySQL, you can combine MAX() with the GREATEST() function to get the biggest value from two or more fields. Here's the syntax for GREATEST: GREATEST(value1,value2,...)

How can I find the minimum value between two values in SQL Server?

How do you select a minimum value in SQL? To find the minimum value of a column, use the MIN() aggregate function; it takes the name of the column or expression to find the minimum value. In our example, the subquery returns the minimum value in the temperature column (subquery: SELECT MIN(temperature) FROM weather ).

How do you find the minimum value between two columns in Python?

To calculate the minimum of each column we'll just call the min() method. Note the usage of the numeric_only parameter that allows to compare only numeric values. A Series representing the smallest value of each column is returned.


2 Answers

based on scalar function (from Tom Hunter):

SELECT ID, (SELECT MIN([date]) FROM (VALUES(Date1),(Date2),(Dateblah)) x([date])) MinDate
FROM TableName
like image 185
Israel Avatar answered Nov 07 '22 13:11

Israel


There is no built in function to return the min/max of two (or more) columns. You could implement your own scalar function to do this.

In SQL Server 2005+ you could use UNPIVOT to turn the columns into rows and then use the MIN function:

CREATE TABLE [X]
(
    [ID] INT,
    [Date1] DATETIME,
    [Date2] DATETIME,
    [Date3] DATETIME
)

INSERT  [X]
VALUES  (0, '09/29/2011', '09/20/2011', '09/01/2011'),
        (1, '01/01/2011', '01/05/2011', '03/03/2010')


SELECT [ID], MIN([Date]) AS [MinDate]
FROM [X]
UNPIVOT (
    [Date] FOR d IN
        ([Date1]
        ,[Date2]
        ,[Date3])
) unpvt
GROUP BY [ID]
like image 27
Tom Hunter Avatar answered Nov 07 '22 13:11

Tom Hunter