Advertisement
amaenta

complicated_sql

Mar 28th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.72 KB | None | 0 0
  1.  
  2. SELECT *, (temp_max.VALUE - temp_second_max.VALUE) AS FUCKING FROM
  3.   (SELECT test.*
  4.    FROM test
  5.           JOIN (
  6.        SELECT *, MAX(TIME) AS max_time
  7.        FROM test
  8.             GROUP
  9.             BY
  10.             event_type
  11.      ) tmp ON test.event_type = tmp.event_type AND test.TIME = tmp.max_time
  12.   ) temp_max
  13. JOIN(
  14.   SELECT test.*
  15.   FROM test
  16.          JOIN (
  17.       SELECT t2.event_type, MAX(t2.TIME) AS max_second_time
  18.       FROM test AS t1
  19.              JOIN test AS t2 ON t1.event_type = t2.event_type AND t1.TIME > t2.TIME
  20.       GROUP BY t2.event_type
  21.     ) tmp ON test.event_type = tmp.event_type AND test.TIME = tmp.max_second_time
  22. ) temp_second_max ON temp_max.event_type = temp_second_max.event_type;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement