Solved

How to order Highest and lowest value rows alternatively in SQL Server ?

Posted on 2016-11-18
4
45 Views
Last Modified: 2016-11-18
Sample data-

CREATE TABLE #emp_records
(
 Id INT,
 Name VARCHAR(80),
 Salary NUMERIC(8, 2)
)
GO
 
INSERT INTO #emp_records
(Id, Name, Salary)
VALUES(1, 'A', 7000),
(2, 'B', 30000),
(3, 'C', 6000),
(4, 'D', 10000),
(5, 'E', 800),
(6, 'F', 500)

Desired Output :-

Id      Name      Salary
2      B      30000.00
6      F      500.00
4      D      10000.00
5      E      800.00
1      A      7000.00
3      C      6000.00
0
Comment
Question by:Mayank Tripathi
  • 2
  • 2
4 Comments
 
LVL 25

Expert Comment

by:Pawan Kumar
ID: 41892727
Do you always have even number in this case ? or we can have odd number also ?
0
 

Author Comment

by:Mayank Tripathi
ID: 41892728
Thanks Pawan, by the way the rows are not fixed.
0
 
LVL 25

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41892731
try..

---

DECLARE @Count INT = 0
SELECT @Count = COUNT(1) FROM emp_records
DECLARE @limit INT = CEILING(@Count / 2.0)

;WITH CTE AS
(
	SELECT * FROM
	(
		SELECT * , ROW_NUMBER() OVER(ORDER BY Salary DESC) rnk FROM emp_records
	)B1 
	WHERE rnk <= @limit
)
,CTE1 AS 
(
	SELECT * FROM
	(
		SELECT *, ROW_NUMBER() OVER(ORDER BY Salary ASC) rnk FROM emp_records
	)B2
	WHERE rnk <= @limit - (@Count % 2.0) 
)
SELECT * FROM CTE c
UNION ALL
SELECT * FROM CTE1
ORDER BY rnk ASC, Salary DESC

Open in new window


Hope it helps !
0
 

Author Closing Comment

by:Mayank Tripathi
ID: 41892793
Thanks.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now