SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


select query


select query

Author
Message
ojrodriguez
ojrodriguez
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 35
I know but someone posted a reply and I just replied just in case.
Thanks
oj



Zidar
Zidar
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 383
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


:-)
ojrodriguez
ojrodriguez
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 35
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;




ojrodriguez
ojrodriguez
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 35
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;




Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search