如何在MySQL中对PARTITION BY使用rank()

时间:2019-08-20 18:48:13

标签: mysql sql window-functions dense-rank

让我们考虑一下三种材料类型,例如('COTTON','LEATHER','SILK'),我想获取dress_id,它具有所有这三种材料类型。我也想对它们进行排名。

有人可以逐步说明如何执行此操作吗? 我列举了几个例子,但我似乎都不清楚。

The output should look something like

DRESS_ID   MATERIAL LAST_UPDATED_DATE RANK
111        COTTON   2019-08-29         1
111        SILK     2019-08-30         2
111        LEATHER  2019-08-31         3
222        COTTON   2019-08-29         1
222        SILK     2019-08-30         2
222        LEATHER  2019-08-31         3
222        LEATHER  2019-09-02         4

在执行此查询时,MYSQL工作台出现错误。 错误代码:1305。FUNCTION等级不存在。

SELECT dress_id,
       rank() over(PARTITION BY dress_id, material ORDER by LAST_UPDATED_DATE asc) as rank
FROM dress_types;

4 个答案:

答案 0 :(得分:0)

SELECT dress_id
     , material
     , LAST_UPDATED_DATE
   rank() over(PARTITION BY dress_id ORDER by LAST_UPDATED_DATE asc) as rank
FROM dress_types

答案 1 :(得分:0)

SELECT T.*,
  CASE WHEN @prev_dress_id != T.dress_id THEN @rank:=1
       ELSE @rank:=@rank+1 
  END as rank,
  @prev_dress_id := T.dress_id as set_prev_dress_id
FROM 
  (SELECT dress_id,material,last_updated_date
  FROM dress_types T1
  WHERE EXISTS (SELECT 1 FROM dress_types E1 WHERE E1.dress_id = T1.dress_ID AND E1.material = 'COTTON')
    AND EXISTS (SELECT 1 FROM dress_types E2 WHERE E2.dress_id = T1.dress_ID AND E2.material = 'SILK')
    AND EXISTS (SELECT 1 FROM dress_types E3 WHERE E3.dress_id = T1.dress_ID AND E3.material = 'LEATHER')
  ORDER BY dress_id asc,last_updated_date asc
  )T,(SELECT @prev_dress_id:=-1)V

内部选择会选择所有3种材料均存在并按dress_id,last_updated_date排序的礼服。 外部将其与prev_dress_id变量连接在一起,该变量可以在每行的末尾设置。 case语句的逻辑根据@prev_dress_id!=或= T.dress_id计算等级。 sqlfiddle

答案 2 :(得分:0)

对于早期版本的MySQL 8.0,您必须使用变量来模拟排名:

SET @rownum := 0;
SET @group_number := 0;
SELECT dress_id, material, last_updated_date, rank FROM (
SELECT @rownum := case 
  when @group_number = dress_id then @rownum + 1 
  else 1
end AS rank, dress_id, material, last_updated_date,
@group_number := dress_id  
FROM dress_types 
ORDER BY 
  dress_id, 
  FIELD(material, 'COTTON', 'SILK', 'LEATHER'), 
  last_updated_date 
) t

请参见demo
结果:

| dress_id | material | last_updated_date   | rank |
| -------- | -------- | ------------------- | ---- |
| 111      | COTTON   | 2019-08-29 00:00:00 | 1    |
| 111      | SILK     | 2019-08-30 00:00:00 | 2    |
| 111      | LEATHER  | 2019-08-31 00:00:00 | 3    |
| 222      | COTTON   | 2019-08-29 00:00:00 | 1    |
| 222      | SILK     | 2019-08-30 00:00:00 | 2    |
| 222      | LEATHER  | 2019-08-31 00:00:00 | 3    |
| 222      | LEATHER  | 2019-09-02 00:00:00 | 4    |

答案 3 :(得分:0)

在早期版本的MySQL中,您可以使用变量或相关的子查询。

因为每件衣服只有少量材料,所以相关的子查询是合理的,尤其是在索引正确的情况下。代码如下:

SELECT d.dress_id, d.material,
       (SELECT COUNT(*)
        FROM dress_types d2
        WHERE d2.dress_id = d.dress_id AND
              d2.last_updated_date <= d.last_updated_date
       ) as rank
FROM dress_types d;

请注意,这是根据您的数据而不是查询实现的逻辑。相应的查询为:

SELECT dress_id,
       rank() over (PARTITION BY dress_id ORDER by LAST_UPDATED_DATE asc) as rank
FROM dress_types;

所需的索引位于dress_types(dress_id, last_updated_date)上。

实际上,只要没有重复项(按日期),它们都是相同的。如果重复,逻辑可能会有所不同。