Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: How can I create a column with the repeating sequence 1-12?

Long story short, I would like to create a column that repeats the pattern 1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,...etc. for (12 * 460343 =) 5524116 rows. Any wisdom on how I could complete this? Thank you!

like image 502
Walker Avatar asked Jul 02 '15 17:07

Walker


People also ask

How do you repeat a column in SQL?

To create a column with repeated data, set the mode of the column to REPEATED in the schema. A repeated field can be accessed as an ARRAY type in Google Standard SQL. A RECORD column can have REPEATED mode, which is represented as an array of STRUCT types.

How do I create a column sequence number in SQL?

The syntax to create a sequence in SQL Server (Transact-SQL) is: CREATE SEQUENCE [schema.] sequence_name [ AS datatype ] [ START WITH value ] [ INCREMENT BY value ] [ MINVALUE value | NO MINVALUE ] [ MAXVALUE value | NO MAXVALUE ] [ CYCLE | NO CYCLE ] [ CACHE value | NO CACHE ]; AS datatype.

How do you make an incremental number in SQL?

The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record. Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5) .

How do you do sequencing in SQL?

Syntax: CREATE SEQUENCE sequence_name START WITH initial_value INCREMENT BY increment_value MINVALUE minimum value MAXVALUE maximum value CYCLE|NOCYCLE ; sequence_name: Name of the sequence. initial_value: starting value from where the sequence starts.


2 Answers

Insert say 48 then select into from self several times. You will get there real fast. It is surprisingly faster than one would think.

If you create a table with an int autoinc column then at end:

delete from table where id>5524116

Edit here you go

    create table idFix
    (   id bigint auto_increment primary key,
        num int not null
    )engine=myisam;

    -- prime it    
    insert into idFix(num) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12);

    -- this is pretty fast, don't laugh
    -- run the following line 19 times
    insert into idFix(num) select num from idFix;

    -- you now have 6.2m rows (6,291,456)
    select count(*) from idFix

    delete from idFix where id>5524116;

    select count(*) from idFix;

    select min(num),max(num) from idFix;

Takes 3 minutes max

Use your helper table then for the love of Pete drop it !
like image 92
Drew Avatar answered Oct 11 '22 15:10

Drew


Use a loop and do some mod division with your counter.

DECLARE @LoopCounter bigint
SET @LoopCounter = 0

CREATE TABLE #YourValues
(
    YourValue_Key int NOT NULL identity (1,1) PRIMARY KEY, 
    YourValue_OneThrough12Repating int
)

WHILE @LoopCounter < 5524116
    BEGIN
        INSERT INTO #YourValues (YourValue_OneThrough12Repating) VALUES ((@LoopCounter % 12) + 1)
        SET @LoopCounter = @LoopCounter + 1
    END

SELECT * FROM #YourValues

DROP TABLE #YourValues
like image 39
LDMJoe Avatar answered Oct 11 '22 17:10

LDMJoe