Avinash Raina
Avinash Raina

Reputation: 13

Get the alternate rows in SQL

I have a table PERSON with a single column GENDER, and values in 6 rows is like:

GENDER

M
M
M
F
F
F

The output should be like

GENDER

M
F
M
F
M
F

What should be the SQL query to get such output? I believe ROWNUMBER() must be used.

Upvotes: 1

Views: 1900

Answers (1)

Squirrel
Squirrel

Reputation: 24763

SELECT GENDER, R = ROW_NUMBER() OVER (PARTITION BY GENDER ORDER BY GENDER)
FROM   PERSON
ORDER BY R, GENDER DESC

Upvotes: 2

Related Questions