Sql Order by value

How can I sort using Select by Sequence?

I mean, I have this data:

column_1   |   column_2
    1      |      a    
    2      |      b    
    3      |      c    
    4      |      d
    4      |      e    
    3      |      f    
    1      |      g    
    2      |      h    

      

And I want to order group 1-4, I mean:

column_1   |   column_2
    1      |      a    
    2      |      b    
    3      |      c    
    4      |      d
    1      |      g    
    2      |      h 
    3      |      f    
    4      |      e    

      

I would like column_1 to be ordered in sequence 1,2,3,4 , then 1,2,3,4 . I mean, I don't want to order this path: 1,1,2,2,3,3,4,4 ...

[[EDIT]]
Column_2 should be Asc, I mean: Order By column_2 Asc

[[EDIT 2]]
I will be using this Select on SQLite. I am trying to find a way using only SQL commons commands.

Sql Fiddle

Does anyone know how this is possible?

+3


source to share


1 answer


If you want to order a sequence, you really want to list each value in column 1 independently for each value. For example:

1     1
2     1
3     1
4     1
4     2
3     2
2     2
1     2
5     1
5     2
5     3

      



I'm not sure if there is an easy way to do this in SQLite. In MySQL, you have to use variables. In other databases, window functions. You can do this correlated subquery:

select column_1, column_2
from (select t.*,
             (select count(*) from table t2 where t2.column_1 = t.column_1 and t2.rowid <= t.rowid
             ) as seqnum
      from table t
     ) t
order by seqnum, column_1;

      

+1


source







All Articles