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 7971119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:33:58+00:00 2026-06-04T07:33:58+00:00

I have a PostgreSQL table that is mostly a bridge table but it also

  • 0

I have a PostgreSQL table that is mostly a bridge table but it also has some extra stuff.

Essentially it holds the information about players in a game. So we have a unique id for this instance of a player in a game. Then an id that is FK to game table, and an id that is FK to player table. There is also some other irrelevant stuff. Something like this:

Table players_games
| id        | 12564
| player_id | 556
| game_id   | 156184

What I want to do is find how many occurrences there are of a player playing with another. So, if player1 is in the same game as player2, they have played together once. There are 2+ players in a game.

So what I want to do is populate a new table, that holds three values: player_lo, player_hi, times_played.

And either have one row for each pair and the number of times they played, or if it ends up being more efficient, a row for each iteration and have the value set as 1 so these can be added together later, maybe distributed. So you might see something like:

p1, p2, 1
p1, p2, 1

And these get reduced later to:

p1, p2, 2

So I was wondering if there was some clever way to do this with SQL, or if there’s SQL that can reduce my programming effort, before starting to write a slightly complex python script to do it.

  • 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-04T07:33:59+00:00Added an answer on June 4, 2026 at 7:33 am

    To do this, you need to do a self join on the player_games table. The first subquery is for the first player, and the second for the second player. The “first” player is the one with the lower player id.

    select pg1.player_id as player1, pg2.player_id as player2, count(*) as num_games
    from (select distinct game_id, player_id
          from  players_games pg
         ) pg1 join
         (select distinct game_id, player_id
          from players_games pg
         ) pg2
         on pg1.game_id = pg2.game_id and
            pg1.player_id < pg2.player_id
    group by pg1.player_id, pg2.player_id
    

    Note that the join condition uses a “<” on the player ids. This is to prevent counting duplicates (so players A,B are not also counted as B,A).

    Also, I added a “distinct” in the inner subqueries just in case a single player might appear more than once for a given game. Perhaps this is not necessary. To be sure, you should have a unique index on the composite key game_id, player_id.

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

Sidebar

Related Questions

In PostgreSQL: I have a Table that has 3 columns: CustomerNum, OrderNum, OrderDate .
I have a table that has three columns: Category, Timestamp and Value. What I
I have a PostgreSQL database at work that is a single table of data.
I have a data file in CSV format that has the some data like
I have a postgres table that has the following fields start_date,duration duration contains any
I have a table B that has a foreign key to table A, and
I have a product table that contains thousands of products. Some products are available
I have an article table on a Postgresql 9.1 database and a trigger that
I have one table that has 2 fields one( container_id ) for numeric type
I have a table that holds phone calls, with the following fields: ID STARTTIME

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.