How to repeat/or duplicate my results?

  • HI All!

    This should be a simple one, but I'm having issues coming up with a solution.

    I have the following dates Jan 2008-Dec 2012

    (i.e. 200801

    200802,

    etc.)

    stored into a table. When I run my query, I simply have 48 rows ( one row for each month). How can I get the 48 rows to repeat, say 100 times?

    Thanks!!!:-)

  • Your Query

    GO 100 🙂

    --------------------------------------------------------------------------------------------------
    I am just an another naive wannabe DBA trying to learn SQL Server

  • HA! That was simple enough.

    Is there a way to make it all one result set, rather than 100 sets. I really need it at least 600 times and don't want to copy and paste 600+ times.:-P

  • Not strictly T-SQL but its a useful feature.

  • Cross join to a table with the number of rows equal to the number of repetitions you need. Numbers tables are useful for that.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Sorry, can you elaborate a bit more. I'm still not having any luck.

  • Nevermind, I got it! I'm a little "Slow" on Friday's. Thanks for the help!:hehe:

  • clarks (11/19/2010)


    Nevermind, I got it! I'm a little "Slow" on Friday's. Thanks for the help!:hehe:

    That's cool. Would you mind posting your final code so others may learn how to do it? Thanks?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Here is one way of doing it:

    DROP TABLE #Temp

    DECLARE @X int

    SET @X = 1

    CREATE TABLE #Temp (Phone varchar(17) null)

    WHILE @X < 101

    BEGIN

    INSERT INTO #Temp(Phone) select Phone from People where Phone = '123-4567'

    SET @X = @X + 1

    END

    select peop.* from People peop

    LEFT JOIN #Temp on #Temp.Phone = peop.Phone

    where Phone = '123-4567'

    --DROP TABLE #Temp (don't forget to clean up)

  • churlbut (11/22/2010)


    Here is one way of doing it:

    DROP TABLE #Temp

    DECLARE @X int

    SET @X = 1

    CREATE TABLE #Temp (Phone varchar(17) null)

    WHILE @X < 101

    BEGIN

    INSERT INTO #Temp(Phone) select Phone from People where Phone = '123-4567'

    SET @X = @X + 1

    END

    select peop.* from People peop

    LEFT JOIN #Temp on #Temp.Phone = peop.Phone

    where Phone = '123-4567'

    --DROP TABLE #Temp (don't forget to clean up)

    That's very procedural. Why not do it faster, and more efficient:

    select Phone

    from People

    cross join (select top 100 from sys.columns) as Multiplier;

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • GSquared (11/22/2010)


    churlbut (11/22/2010)


    Here is one way of doing it:

    DROP TABLE #Temp

    DECLARE @X int

    SET @X = 1

    CREATE TABLE #Temp (Phone varchar(17) null)

    WHILE @X < 101

    BEGIN

    INSERT INTO #Temp(Phone) select Phone from People where Phone = '123-4567'

    SET @X = @X + 1

    END

    select peop.* from People peop

    LEFT JOIN #Temp on #Temp.Phone = peop.Phone

    where Phone = '123-4567'

    --DROP TABLE #Temp (don't forget to clean up)

    That's very procedural. Why not do it faster, and more efficient:

    select Phone

    from People

    cross join (select top 100 from sys.columns) as Multiplier;

    🙂

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply