简体   繁体   English

使用ORDER BY的SQL查询第2部分

[英]SQL Query with ORDER BY Part 2

This is a followup question to: SQL Query with ORDER BY 这是一个后续问题: 使用ORDER BY进行SQL查询

But I think the SQL logic is going to be quite different, so I am posting it as separate question. 但我认为SQL逻辑会有很大不同,所以我将它作为单独的问题发布。

I am trying to extend my sql SELECT query it and having some trouble: 我试图扩展我的SQL SELECT查询它并遇到一些麻烦:

I have the table: 我有桌子:

id    type      radius
-------------------------
1     type1     0.25
2     type2     0.59
3     type1     0.26
4     type1     0.78
5     type3     0.12
6     type2     0.45
7     type3     0.22
8     type3     0.98

and I am trying to learn how to SELECT the second smallest radius for each given type . 我正在尝试学习如何SELECT 每个给定类型第二个最小半径 So the returned recordset should look like: 所以返回的记录集应如下所示:

id    type      radius
-------------------------
3     type1     0.26
2     type2     0.59
7     type3     0.22

(Note: in the referenced question, I was looking for the lowest radius, not the second lowest radius). (注意:在引用的问题中,我一直在寻找最低半径,而不是第二低半径)。

I am assuming I have to use LIMIT and OFFSET , but if I use the MIN() won't that return a distinct record containing the minimum radius? 我假设我必须使用LIMITOFFSET ,但如果我使用MIN()将不会返回包含最小半径的独特记录?

Does anyone have any thoughts on how to attack this? 有没有人对如何攻击这个有任何想法?

Many thanks, Brett 非常感谢,布雷特

You didn't mention your DBMS, so I'll post a solution that works with DBMS that support the standard windowing functions: 您没有提到您的DBMS,因此我将发布一个与DBMS一起使用的解决方案,该解决方案支持标准窗口函数:

SELECT *
FROM (
  SELECT id, 
         type,
         radius, 
         dense_rank() OVER (PARTITION BY type ORDER BY radius ASC) as radius_rank
  FROM radius_table
) t
WHERE radius_rank = 2

You can easily pick the 3rd lowest or 14th lowest as well by adjusting the WHERE condition 通过调整WHERE条件,您可以轻松选择第3个最低或第14个最低

This solution will also work if you have more than one row that qualifies for 2nd lowest (the LIMIT solutions would only show one of them) 如果您有多个符合第二低的行(LIMIT解决方案只显示其中一个),此解决方案也将起作用

This query gives you the 2nd position of a given type 此查询为您提供给定类型的第二个位置

SELECT  *
FROM    `test`.`rads`
WHERE   type = 'type wanted'
ORDER BY `radius` ASC
LIMIT 1, 1

You can mix this in a subquery to fetche a whole list, like this query 您可以在子查询中混合使用它来获取整个列表,例如此查询

SELECT  id, type, radius
FROM    `test`.`rads` t
WHERE   id = (
    SELECT  id
    FROM    `test`.`rads` ti
    WHERE   ti.type = t.type
    ORDER BY `radius` ASC
    LIMIT 1, 1)
ORDER BY radius ASC, id DESC

With this query you can vary the position by changing the LIMIT first parameter 使用此查询,您可以通过更改LIMIT第一个参数来改变位置

I would use the SQL query from your previous answer and add a WHERE instrution in it removing all records containing the 'id' of the matching '1st lowest radius'. 我将使用您之前的答案中的SQL查询,并在其中添加一个WHERE指令,删除包含匹配的“第一个最小半径”的“id”的所有记录。

SELECT t1.id,t1.type,t1.radius FROM table t1 
WHERE radius = ( 
     SELECT MIN(radius) FROM table
     WHERE radius = t1.radius
     AND id not IN (
       SELECT t2.id FROM table t2
       WHERE radius = ( 
         SELECT MIN(radius) FROM table
         WHERE radius = t2.radius
       )
     )
   )

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

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