select query

  • I know but someone posted a reply and I just replied just in case.

    Thanks

    oj

  • This should do the trick:

    SELECT

    CASE

    WHEN Field1 >= Field2 THEN

    CASE WHEN Field1 >= Field3 THEN Field1 ELSE Field3 END

    ELSE

    CASE WHEN Field2 >= Field3 THEN Field2 ELSE Field3 END

    END AS MaxOf3Fields

    FROM MyTable

    🙂

  • Or like this?

    --select the grandmax of the max of three columns

    DECLARE @date1Max AS datetime = (SELECT MAX([orderdate])

    FROM [Sales].[OrderValues]);

    DECLARE @date2Max AS datetime = (SELECT MAX([requireddate])

    FROM [Sales].[OrderValues]);

    DECLARE @date3Max AS datetime = (SELECT MAX([shippeddate])

    FROM [Sales].[OrderValues]);

    --SELECT @date1Max AS orderdate, @date2Max AS requireddate , @date3Max AS shippeddate; --TO CHECK CORRECT DATE

    SELECT CASE

    WHEN @date1Max >= @date2Max AND @date1Max >= @date2Max THEN @date1Max

    WHEN @date2Max >= @date1Max AND @date1Max >= @date3Max THEN @date2Max

    ELSE @date3Max

    END AS GrandMax;

  • Sorry, my previous post included some mistakes.

    here is the corrected code:

    SELECT CASE

    WHEN @date1Max >= @date2Max AND @date1Max >= @date3Max THEN @date1Max

    WHEN @date2Max >= @date1Max AND @date2Max >= @date3Max THEN @date2Max

    ELSE @date3Max

    END AS GrandMax;

Viewing 4 posts - 31 through 33 (of 33 total)

You must be logged in to reply to this topic. Login to reply