Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234

select query Expand / Collapse
Author
Message
Posted Tuesday, December 3, 2013 12:13 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, December 8, 2013 9:31 AM
Points: 19, Visits: 35
I know but someone posted a reply and I just replied just in case.
Thanks
oj



Post #1519358
Posted Wednesday, December 4, 2013 11:13 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 2:28 PM
Points: 18, Visits: 184
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


Post #1519730
Posted Wednesday, December 4, 2013 1:24 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, December 8, 2013 9:31 AM
Points: 19, 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;



Post #1519773
Posted Wednesday, December 4, 2013 3:53 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, December 8, 2013 9:31 AM
Points: 19, 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;



Post #1519821
« Prev Topic | Next Topic »

Add to briefcase «««1234

Permissions Expand / Collapse