In this Blog I will tell you about How you can Sort Data or in other words use Order By in SQL which will work according to condition provided. Earlier I was doing Order By like this:
- select sub.BroadSubjectArea 'Subject Area',count(BroadSubjectAreasID) 'Number Of Colleges' from defBroadSubjectAreas sub
- join datPFRIBroadAreasDetail area on sub.BroadSubjectAreas_ID=area.BroadSubjectAreasID
- group by sub.BroadSubjectArea
- order by [Subject Area]
and I was getting this output:
Now I will show you the query which will order the data but "Others" will be shown in the last:
- select sub.BroadSubjectArea 'Subject Area',count(BroadSubjectAreasID) 'Number Of Companies' from defBroadSubjectAreas sub
- join datPFRIBroadAreasDetail area on sub.BroadSubjectAreas_ID=area.BroadSubjectAreasID
- group by sub.BroadSubjectArea
- order by
- case when sub.BroadSubjectArea = 'Others' then 1 else 0 end,
- sub.BroadSubjectArea
- GO
Now we will see the result:
source : http://www.c-sharpcorner.com/Blogs/46293/sort-order-by-data-in-sql-except-some-certain-row.aspx
No comments:
Post a Comment