Tuesday, 15 October 2013

How to Get Highest and Lowest Values in a Row using SQL SERVER

Hi friends, in this article I will explain about How to Get Highest and Lowest Values in a Row using SQL SERVER.
DECLARE @MARKS TABLE(ID INT PRIMARY KEY,
                     Name VARCHAR(15),
                     MATHS INT,
                     SCIENCE INT,
                     SOCIAL INT)


INSERT INTO @MARKS VALUES(1,'KISHORE',75,95,66)
INSERT INTO @MARKS VALUES(2,'ROJA',96,85,75)
INSERT INTO @MARKS VALUES(3,'KRISH',55,95,80)
INSERT INTO @MARKS VALUES(4,'ANIL',90,60,80)

SELECT M.ID,TT.MaxMarks,TT.MinMarks FROM @MARKS AS M
CROSS APPLY
(
SELECT MAX(ROW) AS MaxMarks,MIN(ROW) AS MinMarks
FROM(
SELECT MATHS UNION ALL
SELECT SCIENCE UNION ALL
SELECT SOCIAL
) AS TEMP(ROW)
) AS TT

The output of the above code as shown in the below figure.

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.