Indiecoder
Indiecoder

Reputation: 186

Create sequence with specific set of values in oracle

I would like to create a sequence in oracle that will consists of two values(1, -1).

Sequence will be 1,-1,1,-1,1,-1

Is it possible to create this type of sequence in oracle that will alternate between this two values only?

Is this possible using standard Create sequence syntax in oracle?

Upvotes: 0

Views: 285

Answers (2)

user330315
user330315

Reputation:

Yes, this is possible:

create sequence seq_alternate 
   minvalue -1
   maxvalue 1
   start with 1
   increment by 2
   nocache
   cycle;

Upvotes: 3

Mureinik
Mureinik

Reputation: 311028

One way to create such a sequence is to raise -1 to different powers. If the power is even, you'll get 1 and if its odd, you'll get -1. E.g.:

SELECT POWER(-1, LEVEL + 1)
FROM   dual
CONNECT BY LEVEL <= 6

SQLFiddle

Upvotes: 1

Related Questions