Finding the lowest value in a table greater than a certain value

前端 未结 6 940
臣服心动
臣服心动 2021-02-09 14:01

Say I have the following data

Name      Value
===============
Small        10
Medium      100
Large      1000

Imagine that these represent the

6条回答
  •  我在风中等你
    2021-02-09 14:35

    SELECT  *
    FROM    (
            SELECT  *
            FROM    (
                    SELECT  *
                    FROM    mytable
                    WHERE   value > 10000
                    ORDER BY
                            value
                    )
            UNION ALL
            SELECT  *
            FROM    (
                    SELECT  *
                    FROM    mytable
                    ORDER BY
                            value DESC
                    )
            )
    WHERE   rownum = 1
    

    This will both efficiently use an index on mytable(value) and COUNT(STOPKEY).

    See this article in my blog for performance details:

    • Selecting lowest value

提交回复
热议问题