we have a table named "Result" have only one column say "Result". It contains 12 rows. First 6 rows contains "pass" and the next 6 rows contains "fail"
Result --------------------- pass pass pass pass pass pass fail fail fail fail fail fail
Now what would be the statement in SQL Query to return the return like below
Expected Result:- pass fail pass fail pass fail pass fail pass fail pass fail
Number of record may be in thousands but Query should work for all
in fact you need: the sequence of: one row from end, one row from beginning,...and so on until all rows get selected
so make all passes as ODD
and all fails as EVEN
, now you can simply select and order by the number you made:
with CTE as
(
select result , row_number() over(partition by result order by result) as rn
from result
)
select result,
case
when result='fail' then rn*2 else rn*2 -1 end rn
from CTE
order by rn
select result from (select row_number() over( order by result ) row_num,result from result where result='fail' union select row_number() over( order by result ) row_num,result from result where result='pass')a order by row_num
Works in Mssql
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.