【问题标题】:Sql Server Max() over partition by - translate into MySqlSql Server Max() over partition by - 翻译成 MySql
【发布时间】:2015-03-06 03:16:34
【问题描述】:

所以我看到 SQL Servers RowNumber() over(partition by id) 从 另一个问题posted here

但这对另一个聚合函数(例如 MAX())没有帮助。

我需要一组行的 Max()。

Cust_Type   Cust_Name   Revenue  Max
Top         A           10000    10000
Top         B           9000     10000 
Top         C           8000     10000 
Bottom      X           5000     7000
Bottom      Y           6000     7000
Bottom      Z           7000     7000

现在我知道我可以进行查询以获取 Max 和 Cust_Type,然后将其连接到整个表以添加最大值,但是有没有办法不进行额外的查询,而是在相同的情况下进行像我引用的帖子那样时尚?

【问题讨论】:

    标签: mysql sql sql-server


    【解决方案1】:

    如果我理解正确,您可以使用用户定义的变量。

    select cust_type, 
           cust_name,
           revenue,
           @max:=IF(@custtype!=cust_type,revenue,@max),
           @custtype:=cust_type
    from yourtable, (select @max:=0, @custtype:='') t
    order by cust_type, revenue desc
    

    【讨论】:

    • 这是完美的。正是我需要的。
    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2020-08-06
    • 2021-12-15
    • 2022-07-13
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多