Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sort data but leaving out a specific data to be the first field on the return results in SQL SERVER

I have a select statement that retrieves data and sorts the data by one of the columns, at the moment all the fields are sorting accordingly but I would like to have a specific row to always be the first and not be affected by the sort. Here is what I have done:

select distinct colID,  
    colA,
    colB,
    colC      
from vw_dATA
where (colID = 1)
order by  colB

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can combine multiple sorting criteria with a CASE statement. For example:

select *
from (
  select distinct colID,  
    colA,
    colB,
    colC,
    my_row_id
  from vw_dATA
  where colID = 1
) x
order by
  case when my_row_id = 1234 then 1 else 2 end, -- first ordering criteria
  colB -- rest of the ordering
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading