Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6165007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:05:24+00:00 2026-05-23T22:05:24+00:00

To start things off, I want to make it clear that I’m not trying

  • 0

To start things off, I want to make it clear that I’m not trying to order by descending order.

I am looking to order by something else, but then filter further by displaying things in a second column only if the value in that column 1 row below it is less than itself. Once It finds that the next column is lower, it stops.

Example:

Ordered by column-------------------Descending Column
353215                                    20
535325                                    15
523532                                    10
666464                                    30
473460                                    20

If given that data, I would like it to only return 20, 15 and 10. Because now that 30 is higher than 10, we don’t care about what’s below it.

I’ve looked everywhere and can’t find a solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:05:25+00:00Added an answer on May 23, 2026 at 10:05 pm

    EDIT: removed the big number init, and edd the counter in ifnull test, so it works in pure MySQL: ifnull(@prec,counter) and not ifnull(@prec,999999).

    If your starting table is t1 and the base request was:

    select id,counter from t1 order by id;
    

    Then with a mysql variable you can do the job:

    SET @prec=NULL;
    select * from (
        select id,counter,@prec:= if(
           ifnull(@prec,counter)>=counter,
           counter,
           -1) as prec
        from t1 order by id
     ) t2 where prec<>-1;
    

    except here I need the 99999 as a max value for your column and there’s maybe a way to put the initialisation of @prec to NULL somewhere in the 1st request.

    Here the prec column contains the 1st row value counter, and then the counter value of each row if it less than the one from previous row, and -1 when this becomes false.

    Update

    The outer select can be removed completely if the variable assignment is done in the WHERE clause:

    SELECT @prec := NULL;
    SELECT
      id,
      counter
    FROM t1
    WHERE
      (@prec := IF(
        IFNULL(@prec, counter) >= counter,
        counter,
        -1
      )) IS NOT NULL
      AND @prec <> -1
    ORDER BY id;
    

    regilero EDIT:
    I can remove the 1st initialization query using a temporary table (left join) of 1 row this way: but this may slow down the query, maybe.

    (...)
    FROM t1 
    LEFT JOIN (select @prec:=NULL as nullinit limit 1) as tmp1 ON tmp1.nullinit is null
    (..)
    

    As said by @Mike using a simple UNION query or even :

    (...)
    FROM t1 , (select @prec:=NULL) tmp1 
    (...)
    

    is better if you want to avoid the first query.

    So at the end the nicest solution is:

    SELECT NULL AS id, NULL AS counter FROM dual WHERE (@prec := NULL) 
    UNION
    SELECT   id,   counter
    FROM t1  
    WHERE   (
       @prec := IF(
          IFNULL(@prec, counter) >= counter,
          counter,
          -1   )) IS NOT NULL
    AND @prec <> -1
    ORDER BY id;
    
    +--------+---------+
    | id     | counter |
    +--------+---------+
    | 353215 |      20 |
    | 523532 |      10 |
    | 535325 |      15 |
    +--------+---------+
    

    EXPLAIN SELECT output:

    +----+--------------+------------+------+---------------+------+---------+------+------+------------------+
    | id | select_type  | table      | type | possible_keys | key  | key_len | ref  | rows | Extra            |
    +----+--------------+------------+------+---------------+------+---------+------+------+------------------+
    |  1 | PRIMARY      | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE | 
    |  2 | UNION        | t1         | ALL  | NULL          | NULL | NULL    | NULL |    6 | Using where      | 
    | NULL | UNION RESULT | <union1,2> | ALL  | NULL          | NULL | NULL    | NULL | NULL | Using filesort   | 
    +----+--------------+------------+------+---------------+------+---------+------+------+------------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to start off by asking for your collective patience... I am trying
So, to start off, I want to point out that I know that these
Let me start off by stating that I know the assignment operator in C#
I'm going to start this off by saying that I am new to GLSL
I want to use Backbone.js for some event handling things, but I am not
Now, first off, I want to understand whether or not its better to use
Strange things happen. I can start Tomcat from both Eclipse and startup.sh. Runnig from
When we start sorting or other things datatables has option to show 'Process' message.
I'm a starting C/C++ programmer. What I want is the following thing: Start program,
I’m trying to make a counter which shows the number of days until we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.