I need to return multiple value in a single SQL query on my postgresql database. Here is my query so far:
SELECT AVG("percent"), MIN("percent"), MAX("percent"), AVG("profit"), MIN("profit"), MAX("profit")
FROM public.log_analyticss
WHERE "buyPlatform" = 'platA' AND
"date" >= '1526356073.6126819'
Data
date sellPlatform profit percent
---------- ------------ ---------- ----------
1526356073.61 platA 0 10.1
1526356073.62 platA 22 11
1526356073.63 platA 3 7
1526356073.64 platA 1 8
1526356073.65 platA 11 9
1526356073.66 platA 12 10
1526356073.67 platA 13 15
Desired Result
date sellPlatform profit percent
---------- ------------ ---------- ----------
1526356073.61 platA 0 10.1 //MIN Profit
1526356073.62 platA 22 11 //MAX Profit
1526356073.63 platA 3 7 //MIN Perc
1526356073.67 platA 13 15 //MAX Perc
//Then somehow I want it return AVG as well if that is every possible. Otherwise, I don't mind running another query to do that.
The issue is that I don't just want the MIN and MAX values. I want the entire row of data from which the MIN and MAX value is taken.
I understand that I am asking for an AVG and a MIN/MAX value which is going to return data in two different formats. I know this is might not be possible. But any help on how to most efficiently do this would be extremely helpful.
Right now I am just pulling the entire dataset into my code and calculating the mean, min and max within my code which I know if very bad and also very very slow. The table has about 8 million rows and the dataset that I am grabbing is about 9000 rows so it is very slow the way I am doing it now.