sql >> Database >  >> RDS >> Mysql

de laatste opeenvolgende rij vinden

SELECT a.id, a.readVal 
FROM consecutiv a
WHERE a.readVal != 
   (SELECT b.readVal 
    FROM consecutiv b 
    WHERE b.id > a.id 
    ORDER BY id ASC
    LIMIT 1)
ORDER BY a.id;

retourneert:

 id | readval
----+---------
  3 |       2
  8 |       3
  9 |       4
 10 |       5
 12 |       6
 14 |       2

voor het geval dat de allerlaatste rij ook nodig is:

SELECT c.id, c.readVal, c.nextReadVal
FROM 
  (SELECT 
   a.id, a.readVal, 
     (SELECT b.readVal
      FROM consecutiv b 
      WHERE b.id > a.id 
      ORDER BY id ASC 
      LIMIT 1) AS nextReadVal
   FROM consecutiv a) AS c
WHERE readVal != nextReadVal OR nextReadVal IS NULL
ORDER BY c.id;

retourneert:

 id | readval | nextreadval
----+---------+-------------
  3 |       2 |           3
  8 |       3 |           4
  9 |       4 |           5
 10 |       5 |           6
 12 |       6 |           2
 14 |       2 |           6
 15 |       6 |



  1. Hoe het jsonb-type van PostgreSQL 9.4 naar float te converteren?

  2. SQL SELECT-syntaxis - weergegeven door DBMS

  3. Wat is de beste manier om verbinding te maken tussen Android en Oracle-database?

  4. PHP MySQL Google Chart JSON - Compleet voorbeeld