-2

I have table like this:

ID NAME
1 juan
2 pedro
3 jose
4 lucas
5 antoni

I need show result like this:

1 juan
3 jose
5 antoni

How should I proceed to show every record in a sequence of 2 ? Thank you

jantoni
  • 3
  • 5

2 Answers2

3

Use Modulo(%) operator

Select * from yourtable Where ID % 2 = 1

This considers ID is sequential

Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
  • Won't deleted records affect this solution though? – Honinbo Shusaku Dec 22 '16 at 15:45
  • My initial thought from his/her results example was thinking he/she wanted odds as well, but looking at the title, it seems he/she wants every next row (though I'm not sure how order is deduced or matters) – Honinbo Shusaku Dec 22 '16 at 15:47
  • @Abdul The OP hasn't indicated that that's a problem – Strawberry Dec 22 '16 at 15:48
  • @Strawberry Understood. I misread "next row" as every other row – Honinbo Shusaku Dec 22 '16 at 15:50
  • @Strawberry at the same time, if I understand secuencia to mean sequence, I interpret his/her statement of "The secuencia is 2." as meaning records in sequence of two or every other row, as I had initially interpreted it. Thus deleting a record would place either an even ID or odd ID next to each other, in which the result set for this solution would not return "every other row" – Honinbo Shusaku Dec 22 '16 at 15:52
  • and if the ID not is sequential?? – jantoni Dec 22 '16 at 15:53
  • @jantoni in that case, I'd loop through the table or use a cursor and manually skip every other iteration, and place them into a table variable or temp table. I'm not too experienced with SQL though, so I'm not sure if that's the best solution – Honinbo Shusaku Dec 22 '16 at 15:55
  • thank you, i need too show rows with sequential 3,4,5, etc – jantoni Dec 22 '16 at 16:03
  • @Abdul Yes, but until the OP clarifies their requirements, we have no way of knowing which is the right strategy. The OP could easily have connstructed a more comprehensive dataset. They chose not to. – Strawberry Dec 22 '16 at 16:04
0
 SELECT * FROM 
 (
 SELECT ID,NAME,ROW_NUMBER()OVER(ORDER BY ID ASC)as ROW
 FROM TABLE
 ) as A
 WHERE ROW % 2 = 0
LONG
  • 4,490
  • 2
  • 17
  • 35