Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2011-01-06 : 19:22:16
|
Hey.I wasn't wondering if i can order the rows of a column but always leave a row that is not necessarily the last one, for the last result.P.e. I may have a,b,c,d,e,f but what i would like to have would bea,b,c,e,f,d .So here the d is set as a last row. |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-01-06 : 20:06:37
|
SELECT * FROM myTable ORDER BY CASE WHEN myColumn='d' THEN 1 ELSE 0 END, myColumn |
 |
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2011-01-07 : 02:20:07
|
Thanks, that did it.Although i don't understand how this is working: order by CASE WHEN myColumn='something' THEN 1 ELSE 0 END |
 |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-01-07 : 06:36:29
|
The CASE expression evaluates a logical condition (A=B, x<123) and returns a value depending if its true or not. In the example I gave, it compares the column to 'd', and returns a 1 if there's a match, or a zero if not. The ORDER BY will sort these values and put the zeros first.Books Online has a full entry on CASE with numerous examples on how to use it. |
 |
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2011-01-09 : 02:50:06
|
I'm familiar with Case but i didn't know this:"The ORDER BY will sort these values and put the zeros first"So thanks. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-09 : 13:10:00
|
quote: Originally posted by sapator I'm familiar with Case but i didn't know this:"The ORDER BY will sort these values and put the zeros first"So thanks.
thats obvious isnt it. As 0 comes before 1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2011-01-09 : 19:29:48
|
It's obvious |
 |
|
|
|
|
|
|