Thread: how to duplicate data for few times by SQL command in PG

how to duplicate data for few times by SQL command in PG

From
tsunghan hsieh
Date:
Hi

I have a table which just has one column as following in Original Table. I wanna duplicate all of data for few times and with same order as following in New Table. Is there anyone who can help me? Thanks

Han

Original Table
23
45
65
22

New Table
23
23
23
45
45
45
65
65
65
65
22
22
22
22

Re: how to duplicate data for few times by SQL command in PG

From
Raymond O'Donnell
Date:
On 22/01/2015 15:37, tsunghan hsieh wrote:
> Hi
>
> I have a table which just has one column as following in Original Table.
> I wanna duplicate all of data for few times and with same order as
> following in New Table. Is there anyone who can help me? Thanks

How about something like this? -

    insert into <new table>
    select <whatever> from <original table>
    cross join generate_series(1, 3)
    order by <whatever>

This assumes that you want each item duplicated the same number of times.

Hope this helps,

Ray.

--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie