问题
I have difficulties in doing the following exercise. I would need to find how frequent is that an id
is not the max_id
in the group with the most amount
. This should be done considering groups that contain at least two different people.
Data comes from two different tables: max_id
comes from table1 (I will call it a
)as well as user
and amount
; id
comes from table2 (b
) as well as group.
From the text above, the conditions should be
(1) a.id<>b.max_id /* is not */
(2) people in group >=2
(3) a.id<> id of max amount
The dataset looks like
(a)
max_id user amount
(b)
group email
From a previous exercise, I had to compute distinct people as follows:
sel a.distinct users
a.max_id
b.id
from table1 as a
inner join table2 as b
on b.id=a.max_id
where
b.max_id is not null
and b.time is null
No information from amount was required in the exercise above. This is the main difference between the two exercises, but the structure and fields are quite similar.
Now, I would need to edit the code above in order to find how frequent is that an id
is not the max_id
in the group with the most amount
. This makes sense only if groups have at least two different persons/users.
I think I will need to join tables to get the id of max amount in a group and count people in a group, but I do not know how to do it.
Any help would be greatly appreciated. Thank you.
Data sample
max_id user amount id group email
12 1 -2000 12 house email1
312 1 0 54 work email1
11 32 -213 11 house email32
41 13 -43 78 work email13
312 53 -650 34 work email53
1 67 -532 43 defense email67
64 76 -9650 98 work email76
For my understanding, what the exercise asks and based on the code above, I should find values for id<>max_id
and having more than 2 users
in a group
(i.e. house, work, defence).
Then, what I would need to select is id <> id of max amount
.
I hope this it can be a bit more clear.
回答1:
assuming yoy have a query as
select t.User, m.Email, m.Model, m.Amount
from my_table m
inner join (
select user, max(amount) max_amount
from my_table
group by user
) t on t.user = m.user
and t.max_amount = m.amount
you can obatin the max di for each amoun using
select max(id), Amount
from (
select m.id, t.User, m.Email, m.Model, m.Amount
from my_table m
inner join (
select user, max(amount) max_amount
from my_table
group by user
) t on t.user = m.user
and t.max_amount = m.amount
) k
and you should obtain the valud of id that are not equal to max id as
select mm.id, t.User, mm.Email, mm.Model, mm.Amount
from my_table mm
inner join (
select user, max(amount) max_amount
from my_table
group by user
) t on t.user = m.user
and t.max_amount = m.amount
inner join (
select max(k.id) max_id, k.Amount
from (
select m.id, t.User, m.Email, m.Model, m.Amount
from my_table m
inner join (
select user, max(amount) max_amount
from my_table
group by user
) t on t.user = m.user
and t.max_amount = m.amount
) k
) kk ON kk.max_id <> mm.id
来源:https://stackoverflow.com/questions/61823394/calculate-variable-of-max-amount-in-a-group