Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7955171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:39:06+00:00 2026-06-04T03:39:06+00:00

I have two tables, custassets and tags . To generate some test data I’d

  • 0

I have two tables, custassets and tags. To generate some test data I’d like to do an INSERT INTO a many-to-many table with a SELECT that gets random rows from each (so that a random primary key from one table is paired with a random primary key from the second). To my surprise this isn’t as easy as I first thought, so I’m persisting with this to teach myself.

Here’s my first attempt. I select 10 custassets and 3 tags, but both are the same in each case. I’d be fine with the first table being fixed, but I’d like to randomise the tags assigned.

SELECT
    custassets_rand.id custassets_id,
    tags_rand.id tags_rand_id
FROM
    (
        SELECT id FROM custassets WHERE defunct = false ORDER BY RANDOM() LIMIT 10
    ) AS custassets_rand
,
    (
        SELECT id FROM tags WHERE defunct = false ORDER BY RANDOM() LIMIT 3
    ) AS tags_rand

This produces:

custassets_id | tags_rand_id 
---------------+--------------
          9849 |         3322  }
          9849 |         4871  } this pattern of tag PKs is repeated
          9849 |         5188  }
         12145 |         3322
         12145 |         4871
         12145 |         5188
         17837 |         3322
         17837 |         4871
         17837 |         5188
....

I then tried the following approach: doing the second RANDOM() call in the SELECT column list. However this one was worse, as it chooses a single tag PK and sticks with it.

SELECT
    custassets_rand.id custassets_id,
    (SELECT id FROM tags WHERE defunct = false ORDER BY RANDOM() LIMIT 1) tags_rand_id
FROM
    (
        SELECT id FROM custassets WHERE defunct = false ORDER BY RANDOM() LIMIT 30
    ) AS custassets_rand

Result:

 custassets_id | tags_rand_id 
---------------+--------------
         16694 |         1537
         14204 |         1537
         23823 |         1537
         34799 |         1537
         36388 |         1537
....

This would be easy in a scripting language, and I’m sure can be done quite easily with a stored procedure or temporary table. But can I do it just with a INSERT INTO SELECT?

I did think of choosing integer primary keys using a random function, but unfortunately the primary keys for both tables have gaps in the increment sequences (and so an empty row might be chosen in each table). That would have been fine otherwise!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:39:07+00:00Added an answer on June 4, 2026 at 3:39 am

    Note that what you are looking for is not a Cartesian product, which would produce n*m rows; rather a random 1:1 association, which produces GREATEST(n,m) rows.

    To produce truly random combinations, it’s enough to randomize rn for the bigger set:

    SELECT c_id, t_id
    FROM  (
       SELECT id AS c_id, row_number() OVER (ORDER BY random()) AS rn
       FROM   custassets
       ) x
    JOIN   (SELECT id AS t_id, row_number() OVER () AS rn FROM tags) y USING (rn);
    

    If arbitrary combinations are good enough, this is faster (especially for big tables):

    SELECT c_id, t_id
    FROM   (SELECT id AS c_id, row_number() OVER () AS rn FROM custassets) x
    JOIN   (SELECT id AS t_id, row_number() OVER () AS rn FROM tags) y USING (rn);
    

    If the number of rows in both tables do not match and you do not want to lose rows from the bigger table, use the modulo operator % to join rows from the smaller table multiple times:

    SELECT c_id, t_id
    FROM  (
       SELECT id AS c_id, row_number() OVER () AS rn
       FROM   custassets -- table with fewer rows
       ) x
    JOIN  (
       SELECT id AS t_id, (row_number() OVER () % small.ct) + 1 AS rn
       FROM   tags
           , (SELECT count(*) AS ct FROM custassets) AS small
       ) y USING (rn);
    

    Window functions were added with PostgreSQL 8.4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: one table (okay, it's a view), vComputer, lists many computers
I have two tables, orders and old_orders. Each table has some similar fields and
I have two tables, I want to transfer all data from the first table
I have two tables like Parent-Child. In Parent table, there are 7211 records. In
I have two tables, like these: Table People: VARCHAR Name INTEGER Age Table Message
I have two tables: projects and supplies. Projects have many supplies, like this: has_many
I have two tables that I want to join into one table and use
I have two tables, for example: Table A Table B ======= ======= Name |
I have two tables: a schedule table that contains information about how an employee
I have two tables with this structure: Table one: ID Description Table two: ID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.