Get MAX and MIN values of each rows in sql server

I have a set of rows with values

enter image description here

  • Passing a checkbox value to a SQL Server stored procedure
  • SQL Server Sum Issue weird behaviour
  • Split comma delimited string and insert to a table (int)
  • Connecting to MS SQL Server with Windows Authentication using Python?
  • Should every User Table have a Clustered Index?
  • How to measure query duration without showing results of query?
  • I want the following OutPut with individual Max and min values

    enter image description here

    Sorry for the poor prntsceen. i dont know how to draw tables in stackoverflow.

    4 Solutions collect form web for “Get MAX and MIN values of each rows in sql server”

    select value1, value2, value3, value4,
           [min]=(select min(value) from (
                  select value1 union all
                  select value2 union all
                  select value3 union all
                  select value4) X(value)),
           [max]=(select max(value) from (
                  select value1 union all
                  select value2 union all
                  select value3 union all
                  select value4) Y(value))
      from tbl;
    

    To recognize NULLs as min values, use the below instead

    select value1, value2, value3, value4,
           [min]=(select TOP(1) value from (
                     select value1 union all
                     select value2 union all
                     select value3 union all
                     select value4) X(value)
                  ORDER BY value ASC),
           [max]=(select TOP(1) value from (
                     select value1 union all
                     select value2 union all
                     select value3 union all
                     select value4) X(value)
                  ORDER BY value DESC)
      from tbl;
    

    Try this one –

    DECLARE @temp TABLE
    (
          Value1 INT
        , Value2 INT
        , Value3 INT
        , Value4 INT
    )
    
    INSERT INTO @temp (Value1, Value2, Value3, Value4)
    VALUES 
        (NULL, 1, 1, NULL),
        (NULL, 1, 2, NULL),
        (NULL, 2, 2, NULL),
        (NULL, 2, 2, NULL),
        (1, 1, 1, 1),
        (2, 2, 1, 2),
        (1, 1, 1, NULL),
        (2, 2, 3, 2),
        (2, 2, 2, 2),
        (1, 1, 1, 1)
    
    SELECT
          Value1
        , Value2
        , Value3
        , Value4
        , MinValue = (
            SELECT TOP 1 value 
            FROM (
                SELECT value = value1
                  UNION
                SELECT value2
                  UNION
                SELECT value3
                  UNION
                SELECT value4
            ) mn
            ORDER BY value
        )
        , MaxValue = (
            SELECT MAX(value) 
            FROM (
                SELECT value = value1
                  UNION
                SELECT value2
                  UNION
                SELECT value3
                  UNION
                SELECT value4
            ) mx
        )
    FROM @temp
    

    Results window:

    proff

    Please try:

    declare @T table
    (
      id int, 
      num1 int, 
      num2 int, 
      num3 int,  
      num4 int,   
      num5 int
    )
    
    insert into @T values
    (1,     6,      51,      NULL,      99,      34),
    (2,     222,    251,     24,     299,     234),
    (3,     NULL,   NULL,    NULL,   NULL,    NULL)
    
    select * From @T
    
    SELECT id,
    (SELECT 
    (CASE WHEN (num1+num2+num3+num4+num5) IS NULL THEN NULL 
        ELSE MIN(v) end) 
    FROM (VALUES (num1), 
                (num2), 
                (num3), 
                (num4), 
                (num5)) AS value(v)) as [MinVal],
    (SELECT MAX(v) 
    FROM (VALUES (num1), 
                (num2), 
                (num3), 
                (num4), 
                (num5)) AS value(v)) as [MaxVal]
    FROM @T
    
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.