如何根据类型和2列获取最高价值的记录

时间:2019-05-15 07:55:56

标签: mysql sql

所以我这里有一个名为my_table的表

| userid | gender | year | money |
| ------ | ------ | ---- | ----- |
| 1      | B      | 12   | 50    |
| 1      | B      | 24   | 150   |
| 1      | B      | 36   | 5     |
| 1      | G      | 0    | 0     |
| 1      | G      | 12   | 120   |
| 1      | G      | 48   | 120   |
| 2      | B      | 12   | 23    |
| 2      | B      | 24   | 56    |
| 2      | G      | 36   | 23    |
| 3      | G      | 0    | 234   |
| 3      | G      | 12   | 34    |
| 4      | G      | 0    |  0    |

我想根据性别返回最高可用的行。 因此,例如,性别B的回报表如下

| userid | gender | year | money |
| ------ | ------ | ---- | ----- |
| 1      | B      | 24   | 150   |
| 2      | B      | 24   | 56    |

性别G的回报表如下

| userid | gender | year | money |
| ------ | ------ | ---- | ----- |
| 1      | G      | 12   | 120   |
| 1      | G      | 48   | 120   |
| 2      | G      | 36   | 23    |
| 3      | G      | 0    | 234   |
| 4      | G      | 0    | 0     |

但是,由于性别G的表具有重复的货币值,因此我希望它返回年份最高的行。 性别G的预期表是

| userid | gender | year | money |
| ------ | ------ | ---- | ----- |
| 1      | G      | 48   | 120   |
| 2      | G      | 36   | 23    |
| 3      | G      | 0    | 234   |
| 4      | G      | 0    | 0     |

我尝试过的方法:http://sqlfiddle.com/#!9/5433cbc/2 我可以获取性别B的表格,但不能获取性别G的表格。

我要实现的目标是生成一个将上述两个性别表合并为一个表。例子

| userid | year_b | max_money_for_b | year_g | max_money_for_g |
| ------ | ------ | --------------- | ------ | --------------- |
| 1      | 24     | 150             | 48     | 120             |
| 2      | 24     | 56              | 36     | 23              |
| 3      | null   | null            | 0      | 234             |
| 4      | null   | null            | 0      | 0               |

3 个答案:

答案 0 :(得分:1)

如下所示,我认为您会得到正确的结果。

select
    m.userid,
    m.gender,
    tmp1.m_money as money,
    tmp2.m_year as year
from
    my_table m
join
(
select
    gender,
    userid,
    max(money) as m_money
from
    my_table
group by
    gender,
    userid
) tmp1 on m.gender = tmp1.gender and m.userid=tmp1.userid and m.money=tmp1.m_money
join
(
select
    gender,
    userid,
    money,
    max(year) as m_year
from
    my_table
group by
    gender,
    userid,
    money
) tmp2 on m.gender = tmp2.gender and m.userid=tmp2.userid and m.money=tmp2.money and m.year=tmp2.m_year
where m.gender='G'
order by m.gender,m.userid

如果使用PostgreSQL或MySQL大于8.0的版本,事情会变得更简单,您可以使用window函数来实现您的要求。

select * from 
(
select
    user_id,
    gender,
    money,
    year,
    row_number() over(partition by userid,gender order by money desc,year desc) as sort
from
    my_table
) tmp where sort = 1

答案 1 :(得分:1)

我认为相关子查询是MySQL中最简单的方法:

select t.*
from my_table t
where t.gender = 'G' and
      (t.year, t.money) = (select t2.year, t2.money
                           from my_table t2
                           where t2.userid = t.userid and
                                 t2.gender = t.gender
                           order by t2.money desc, t2.year desc
                           limit 1
                          );

答案 2 :(得分:0)

从您的where条件中删除条件gender = 'G'

DEMO

select t.* from my_table t where
not exists (
    select 1 from my_table where userid = t.userid and money > t.money
        or (money = t.money and year > t.year)
    )