Odd Row Queries SQL SERVER

2

I need to create query script that returns from the table only its odd rows, sorted in ascending order:

DECLARE @table TABLE (coluna1 varchar(50))
INSERT INTO @table
VALUES ('Anthony'),('Miguel'),('Benjamin'),('Lucca'),('Enzo'),('Martim'), ('Noah'),('Gael'),('Henrique'),('Heitor'),('Nícolas'),('Bernardo'), ('Filipe'),('Arthur'),('Apolo'),('José'),('João'),('Antônio'), ('Vicente'),('Alice'),('Luna'),('Valentina'),('Isabela'),('Larissa' ), ('Laura'),('Antonella'),('Victoria'),('Julia'),('Manuela'),('Ana'), ('Camila'),('Beatriz'),('Elisa'),('Sophia'),('Mayara'),('Maria')

Note: I can not change the above script by creating another field as id.

    
asked by anonymous 23.11.2017 / 16:06

1 answer

4

Use ROW_NUMBER to enumerate the rows, divide the line by 2 where the rest of that division is 1 . The % (MOD) operator will be necessary in this case.

Notice that I used a% uncorrected%, that is, a subConsulta where the external result depends on the internal result.

An example very similar to yours;

CREATE TABLE LINHASIMPARES (TEXTO VARCHAR(10) );


INSERT INTO LINHASIMPARES (TEXTO)
VALUES('MARCONI'),('MAGNO'), ('ALEX');


SELECT L.TEXTO,
       L.LINHA
FROM
  (SELECT TEXTO,
          ROW_NUMBER() OVER(
                            ORDER BY TEXTO ASC) AS LINHA
   FROM LINHASIMPARES) L
WHERE L.LINHA % 2 = 1;

SqlFiddle

    
23.11.2017 / 16:17