简体   繁体   English

我想调整一个只获得排名第一的查询

[英]I want to tune a query that gets only the 1st in the ranking

row_number query question. row_number 查询问题。

To get the data where rn is 1, you can wrap the query below and import it with where rn = 1.要获取 rn 为 1 的数据,您可以包装下面的查询并使用 where rn = 1 导入它。

Is there a way to get only those whose rn is 1 in the current query??有没有办法只获取当前查询中 rn 为 1 的那些?

SELECT a1.member_no 
     , row_number() OVER (PARTITION BY a1.member_no ORDER BY a1.avg_hit_rate desc , a1.top_hit_cnt ) as rn
     , a1.join_no
FROM ht_typing_contents_join_log a1
WHERE a1.reg_date >= STR_TO_DATE(CONCAT( date_format(now(), '%Y%m%d' ) , '000000'), '%Y%m%d%H%i%s')
AND a1.reg_date <= STR_TO_DATE(CONCAT( date_format(now(), '%Y%m%d' ) , '235959'), '%Y%m%d%H%i%s')
and a1.success_yn = 'Y'
AND a1.len_type = '1'

Yes, you should subquery what you currently have and restrict to rn = 1 :是的,您应该子查询您当前拥有的内容并限制为rn = 1

WITH cte AS (
    SELECT a1.member_no,
           ROW_NUMBER() OVER (PARTITION BY a1.member_no
                              ORDER BY a1.avg_hit_rate DESC, a1.top_hit_cnt) AS rn,
           a1.join_no
    FROM ht_typing_contents_join_log a1
    WHERE DATE(a1.reg_date) = CURDATE() AND
          a1.success_yn = 'Y' AND
          a1.len_type = '1'
)

SELECT member_no, join_no
FROM cte
WHERE rn = 1;

What I want is below.我想要的在下面。

SELECT a1. member_no
      , row_number() OVER (PARTITION BY a1.member_no ORDER BY a1.avg_hit_rate desc , a1.top_hit_cnt ) as rn
      , a1. join_no
FROM ht_typing_contents_join_log a1
WHERE a1.reg_date >= STR_TO_DATE(CONCAT( date_format(now(), '%Y%m%d' ) , '000000'), '%Y%m%d%H%i%s')
AND a1.reg_date <= STR_TO_DATE(CONCAT( date_format(now(), '%Y%m%d' ) , '235959'), '%Y%m%d%H%i%s')
and a1.success_yn = 'Y'
AND a1.len_type = '1'
AND rn = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM