title: SQL sort equal values using case
body: <p>A similar question is already asked <a href="https://stackoverflow.com/questions/13340648/sql-need-to-sort-the-value-using-group-by-or-any-other-way">here</a>. But current scenario is little bit complex than previous. In the example if same <code>Itime</code> then we can can sort by case but if <code>Itime</code> and result is same then how can I sort. </p>
  
  <p>My question is, here the result ID:  3,5,6,1,2,7,8,4. Why it is 2,7,8 for fail case .</p>
  
  <p>Why it is not 8,2,7? </p>
  
  <p>If I want the expected result like: 3,5,1,6,8,2,7,4 how can I proceed? </p>
  
  <p>Please run the below commands and help me to sort. Thanks in advance.</p>
  
  <pre><code>if object_id('tempdb.dbo.#temp321','U') is not null
     drop table tempdb.dbo.#temp321
  
  create table #temp321(id int, uname varchar(50), current_point int, 
                        previous_point int, ITime datetime, Result varchar(10))
  
  INSERT into #temp321 values('1','a','50','40','2012-11-12 13:12:28.103','pass')
  INSERT into #temp321 values('2','b','15','10','2012-11-12 13:12:28.103','fail')
  INSERT into #temp321 values('3','c','71','70','2012-11-12 12:58:30.000','pass')
  INSERT into #temp321 values('4','d','34','30','2012-11-12 13:12:28.103','withdraw')
  INSERT into #temp321 values('5','e','40','35','2012-11-12 12:58:41.360','withdraw')
  INSERT into #temp321 values('6','f','65','60','2012-11-12 13:12:28.103','pass')
  INSERT into #temp321 values('7','g','20','15','2012-11-12 13:12:28.103','fail')
  INSERT into #temp321 values('8','h','10','7','2012-11-12 13:12:28.103','fail')
  
  
  select 
      ID 
  from 
      #temp321 
  ORDER BY 
      ITime ASC,
      CASE Result 
         WHEN 'pass' THEN 1 
         WHEN 'fail' THEN 2 
         WHEN 'withdrow' THEN 3 
      END 
  
  drop table #temp321 
  </code></pre>
  
  <ul>
  <li>Current output ID: 3,5,6,1,2,7,8,4</li>
  <li>Expected Output ID: 3,5,1,6,8,2,7,4</li>
  </ul>