S
S
Syndr0me2019-07-19 11:38:17
PostgreSQL
Syndr0me, 2019-07-19 11:38:17

How to split a table with users by teams several times?

There is an entity users
5d318067b6d68584613368.png
Let's say there are 100 users in this stage field command = null.
Under certain conditions, it is necessary to update the rows so that the user with the lowest points becomes the same number in the team as the user with the highest points , and so on for each of the next points in descending order . In the end, it turns out 50 teams of 2 people. How is it easier to do this, write logic on node and do udpate on each line, or can it be done using postgre?
Next, you need to do the same, only teams are combined instead of people. That is, from 1 team, where 2 people, you get one team with 4 people in it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Edward, 2019-07-21
@Syndr0me

I did not understand the meaning of the number in points, but I believe that it is just random)
In this case, it seems like this:
This command will sort all users by points in ascending order, and add a sequential number to them so that the user with row_number = 1 will have the lowest points and the highest row_number will have the highest.
Thanks to the serial key, we can easily make pairs in a cycle:

for i in range ({число юзеров}/2)
    USER (row_number = i) <-> USER (row_number = 100 - i)

It's up to you to decide where to assemble the command together)
But I think it's best to write a function in postgres for these purposes, for example, in pl / PgSQL.
The function can loop through and generate a table of command pairs on the fly.
https://postgrespro.ru/docs/postgresql/11/plpgsql-...

K
Konstantin Tsvetkov, 2019-07-19
@tsklab

Since pointsit is not unique, the grouping will be random. And it can't be done with a single request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question