SELECCIONES MÚLTIPLES - MISMA TABLA

Aquí va...

I have 5 records in a table entitled MyTable with a single field called Se. The 5 records contain the following values (1-5):

se=1 se=2 se=3 se=4 se=5

I want to have the records returned to me as follows:

SELECT * FROM MyTable WHERE se >= 3
UNION
SELECT * FROM MyTable WHERE se < 3
ORDER BY se ASC

My objective is to get records returned as:

3,4,5,1 2

but naturally I get...

1,2,3,4,5

Can you help me? Can MSSQL Server even do this?

Gracias de antemano por cualquier ayuda.

preguntado el 27 de agosto de 11 a las 18:08

3 Respuestas

Prueba esta

SELECT 1, * FROM MyTable WHERE se >= 3
UNION ALL
SELECT 2, * FROM MyTable WHERE se < 3
ORDER BY 1, se ASC

Respondido 27 ago 11, 22:08

PERFECT---THIS WORKS! Thanks a lot. - Robert Cezar

You can use a single select and a condition in the sorting:

select *
from MyTable
order by (case when se >= 3 then 0 else 1 end), se

(The parentheses around the case is not needed, I just added them to make the code clearer.)

Respondido 27 ago 11, 22:08

If you're looking for a hack for that específico escenario:

SELECT * FROM MyTable ORDER BY (se + 2) % 5

Example on PostgreSQL:

$ WITH MyTable(se) AS (VALUES
$     (1), (2), (3), (4), (5)
$ )
$ SELECT * FROM MyTable ORDER BY (se + 2) % 5;
 se
----
  3
  4
  5
  1
  2
(5 rows)

Respondido 27 ago 11, 22:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.