I want to get the MAX value of close to 20 fields in my database. My issue is that I also want some other fields with all of these MAX values. I'm using PostgreSQL.
Here is an example:
Match Table definition
CREATE TABLE "matches" (
"id" int4 NOT NULL DEFAULT nextval('match_players_id_seq'::regclass),
"kills" int4 NOT NULL,
"deaths" int4 NOT NULL,
"assists" int4 NOT NULL,
"gamemode" int4 NOT NULL,
PRIMARY KEY ("id")
);
Match table example data
id | kills | deaths | assists | gamemode |
---|---|---|---|---|
1 | 0 | 0 | 3 | 1 |
2 | 1 | 0 | 2 | 2 |
3 | 1 | 12 | 0 | 3 |
4 | 7 | 2 | 27 | 1 |
5 | 1 | 4 | 27 | 2 |
6 | 2 | 3 | 1 | 3 |
INSERT INTO "matches" ("id", "kills", "deaths", "assists", "gamemode") VALUES
(1, 0, 0, 3, 1),
(2, 1, 0, 2, 2),
(3, 1, 12, 0, 3),
(4, 7, 2, 27, 1),
(5, 1, 4, 27, 2),
(6, 2, 3, 1, 3);
Result I want (Table or JSON style) from the above data
what | amount | gamemode | id |
---|---|---|---|
"kills" | 7 | 1 | 4 |
"deaths" | 12 | 3 | 3 |
"assists" | 27 | 1 | 4 |
{
"maxKills": {"id": 4, "kills": 7, "gamemode": 1},
"maxDeaths": {"id": 3, "deaths": 12, "gamemode": 3}
"maxAssists": {"id": 4, "assists": 27, "gamemode": 1} // Get the first one if 2 are equal
}
This is simplified. Here is a little pastebin to give you an idea of the real query: https://pastebin.com/eT8MNKKe
And another pastebin with the @Erwin answer's implementation: https://pastebin.com/2B8imJTj
I already posted this question two years ago but I used a NoSQL database (Mongo): Get objects containing max values for multiple fields using aggregation in mongodb
Here is my idea, I don't know if it's the best one:
Do 2 queries. The first one to get the MAX value of the 20 columns, and the second one to fetch the rows with a WHERE clause on the 20 aggregated MAX values. I'm just not sure how to do when multiple rows have the same MAX value.