SQL Sorting Data into a Template

Not sure if this is possible, but there may be a creative approach ...

Given this data in SQL Server 2005:

AAA
AAA
in
In
the CCC
the CCC
DDD
DDD

How can I return a result set sorted by template as follows:

AAA
in
the CCC
DDD
AAA
in
the CCC
DDD

+5
source share
3 answers

If your column was called "col" and your table was called "table", I would try something like this:

WITH Indexes AS (
    SELECT 
    ROW_NUMBER() OVER (PARTITION BY col ORDER BY col) as __row,
    col
    FROM table)
SELECT col
FROM Indexes
ORDER BY __row, col;
+5
source

, , Oracle , ROWNUM .

:

  • rownum modulo 2

, , .

0

Mysql if you have a table T (varchar); with the data you provided:

select @t:=a from T order by @t <> a;

works: -)

0
source

All Articles