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

  • SEARCH
  • Home
  • 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 3439300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:17:35+00:00 2026-05-18T08:17:35+00:00

For each parent that exists I would like to select 1 child row randomly.

  • 0

For each parent that exists I would like to select 1 child row randomly. The table structure looks like this:

parent
  id

child
   parent_id
   id
   rank

where there are 1..N child records for each parent and rank is unique per parent going from 1 to N.

The output should be like:

parent    child    rank
--------+-------+-------
 1        34       7
 2        56       8
...

With each parent producing one child row. (This will serve as a basis for Bernoulli Trials.)

(PostgreSQL has a random() function that gives a number between 0 and 1.)

  • 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-05-18T08:17:35+00:00Added an answer on May 18, 2026 at 8:17 am

    not familiar at all with postgresql, but perhaps something like this?

    SELECT  DISTINCT ON (parent_id) parent_id as "parent", id as "child", rank
    FROM    child c
    ORDER BY
            parent_id, random()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple parent/child tables. Contract is the parent table. Each contract can
I have a table with two child tables. For each record in the parent
I'm trying to test if each specified parent node has one specified child node.
Using parent child relationship where a parent can have children while each child has
I have a tree structure where each Node has a parent and a Set<Node>
I have implemented inheritance for two parent classes called Table and Field . Each
I have this bit of JavaScript... 15 $('.ajax_edit_address').each(function() { 16 $(this).ajaxForm({ 17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),
[Previous essay-title for question] Oracle SQL: update parent table column if all child table
I'm using jquery to parse some HTML, something like: $(html).contents().each(function(){ var element = this.tagName;
I've got a MySQL table that acts like a nested set in order to

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.