Repeat a value for 'n' times

  • Hi guys,

    I have a table with a column id_log that has different values:

    | id_log |

    +-------+

    1

    2

    3

    I want each value to repeat as many times I want. Let`s say I want each value to repeat 2 times.I`ll have the the following result:

    | id_log |

    +-------+

    1

    1

    2

    2

    3

    3

    Can this be made somehow in SQL ?

    Please help!

  • DECLARE @multiplicator INT = 3;

    --SELECT 1 AS ID

    --INTO dbo.id_log

    --UNION ALL

    --SELECT 2

    --UNION ALL

    --SELECT 3;

    WITH CTE_Tally AS

    (

    SELECT TOP (@multiplicator) n = ROW_NUMBER() OVER (ORDER BY a.object_id)

    FROMmaster.sys.columns a

    CROSS JOINmaster.sys.columns b

    )

    SELECT ID FROM

    dbo.id_log

    CROSS JOIN CTE_Tally

    ORDER BY ID

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Nice!

    Thanks !

Viewing 3 posts - 1 through 2 (of 2 total)

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