Getting row with MAX value together with SUM - sql

I have a PostgreSQL table example with three columns: a INT, b INT, c TEXT.
For each value of a I want the c with the highest value of b, together with the sum of all b. Something like (if there was an ARGMAX function):
SELECT a, ARGMAX(c for MAX(b)), SUM(b) FROM example GROUP BY a
I've found a lot of solutions with varying techniques to get the ARGMAX bit, but none of them seem to use GROUP BY, so I was wondering what we most efficient way would be to capture the SUM (or other aggregate functions) as well.

This can be easily achieved using window functions:
SELECT a, b, c, s
FROM (
SELECT a, b, c,
ROW_NUMBER() OVER (PARTITION BY a ORDER BY b DESC) AS rn,
SUM(b) OVER (PARTITION BY a) AS s
FROM example) AS t
WHERE t.rn = 1
ROW_NUMBER enumerates records within each a partition: the record having the highest b value is assigned a value of 1, next record a value of 2, etc.
SUM(b) OVER (PARTITION BY a) returns the sum of all b within each a partition.

Related

How to select * in addition to group by?

Consider a PostgreSQL table with fields a-z
a, b, c ... z
-------------
5, 6, 2 ... 9
5, 6, 3 ... 1
I'd like to do a group on fields a,b and keep only records where b was maximum.
SELECT a, max(b) as b, c, d, e ... z
FROM table
GROUP BY a, b
This works fine, but it's annoying to have to type out all the values in SELECT. I'd much rather do something like
SELECT max(b) as b, *
FROM TABLE
But doing so gives error
[42803] ERROR: column "table.id" must appear in the GROUP BY clause or
be used in an aggregate function.
Any idea how to avoid having to type all the column names in a lengthy table when doing a groupby operation?
You can use rank():
select t.*
from (select t.*, rank() over (partition by a order by b desc) as seqnum
from t
) t
where seqnum = 1;
Actually, in Postgres, the fastest method is usually distinct on:
select t.*
from t
order by a, b desc;
With an index on (a, b desc) this should be the fastest method.
Gordon Linoff's answer put me on the right track, namely using distinct on. This works in postgres
SELECT DISTINCT ON (a, b) *
FROM table
ORDER BY a, b DESC
Basically it lists the distinct rows of (a,b) and sorts them in order, hence taking only the first or last value depending on sort order. Actually surprised this works...

RANK repeating data set

Assuming that I have data like the ones in columns A and B, how can I rank them like column C? I have tried multiple varieties of RANK and NTILE but have been unsuccessful. Thank you.
Note: There are not always 3 rows for each group, it varies.
SQL tables are inherently unordered. There is no distinguishing between the 1st and 4th row, with the data as you've presented. You can generate an equivalent result set, but the ordering may differ.
Simple arithmetic may do the trick:
select a,
( row_number() over (order by a) + 2) / 3 ) as
from t
order by a, b, c;
A better method uses the b column:
select a,
row_number() over (partition by b order by a) as c
from t
order by a, c;
you can use ntile as below:
Select *, ntile(2) over(order by (Select NULL)) from #data
Instead of (Select NULL) you can provide any other valid ordering column based on your data

Sorted SQL groups

I was trying to do something like:
SELECT
a, b, c, MAX(d)
FROM
table -- table with 4 columns a, b, c and d
GROUP BY
a, b
I would like to have c as an additional value from the table that I do not want to group by, but that distinguish rows within groups. My problem is that GROUP BY makes c look like the first rows from groups and not the ones that really contain
d = MAX(d)
in the table.
ORDER BY is applied to the whole result, so it's not an option. Can I achieve that in any other way than sorting the table prematurely (as a subquery) and then applying the grouping? Would that work in every SQL engine? Do standards define such behaviors?
Edit1:
I tested something like:
SELECT
t.*,
MAX(d) AS v
FROM
(SELECT
a, b, c, d
FROM
table
ORDER BY
d DESC) AS t
GROUP BY
a, b
and it works... but I do not think anybody can guarantee that the sort order will also be applied to the group rows... - maybe it works this way in MySQL, but how will it go with Oracle or PostgreSQL?
This is ANSI SQL:
SELECT a,
b,
c,
MAX(d) over (partition by a,b) as max_d
FROM the_table
This will still return all rows from the table. The max value will repeated for every row that is returned. If you want to get only the rows with the max value you need to wrap this in a derived table:
select a,b,c,d
from (
SELECT a,
b,
c,
d,
MAX(d) over (partition by a,b) as max_d
FROM the_table
) t
where d = max_d;
That will return multiple rows if the same max value occurs more than once. If you only want a single row for each max value you need to use row_number()
You can use
select x.*,y.c from
(SELECT a, b, MAX(d) as d FROM table GROUP BY a, b) x,(select c,d from table) y
where x.d = y.d

Can I add aggregated column without performing a join?

I have a table table1 with three columns a, b, c. I am creating another column by doing a group by on c and some function func(a,b) as d giving me view1. In order to add the column d to table1, the only thing I can think of is to perform a join between view1 and table1. However, both of them have millions of rows and it gets really slow. Is there any other way without joining them? It looks intuitively that it should be possible.
Here is a snippet of the script
with
found_mean
as
(select sum(count*avg)/sum(count) as combined_avg , b from view_1 group by b),
view_1_m
as
(select combined_avg , count , avg, variance , found_mean.b from found_mean , view_1 where found_mean.b = view_1.b),
Depending on what your function is, you can use window functions (sometimes called analytic functions). For instance, if you wanted the maximum value of b for a given a:
select a, b, c, max(b) over (partition by a) as d
from table1;
Without more information, it is hard to be more specific.
EDIT:
You should be able to do this with analytic functions:
select count , avg, variance,
(sum(count * avg) over (partition by b) /
sum(count) over (partition by b)
) as weighted_average
from view_1;

Top 3 Max entries for a Combination which a condition

I am new to sql side. so if this questoin sound very easy then please spare me. I have a 4 coloumns in a sql table.Let say A,B,C,D . For any BC combination I may get any number of rows. I need to get at max 3 rows (which inturn give me 3 unique value of A for that BC ombination) for these selected rows i should have Top 3 Max value of D. As compare to other entries for that BC combination.
So there can be any number of BC combination so the above logic should imply to all of them.
Most databases support ranking functions. With these, you can do what you want as follows:
select A, B, C, D
from (select t.*,
row_number() over (partition by B, C order by D desc) as seqnum
from t
) t
where seqnum <= 3
order by B, C, D desc
The row_number() function creates a sequencial number. This number starts at "1" in very B,C group, and is ordered by the value of D descending.