![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
#4
| |||
| |||
|
|
"s" in result sets was fixed to 2. So, start_s in my previous example is not necessary. Here is another simplified example: ------------------------------ Commands Entered ------------------------------ WITH t(s,t) as ( VALUES (1,1), (2,2), (3,3), (2,4) , (2,5), (3,6), (2,7), (0,8) ) SELECT 2 AS s , MIN(t) AS t , MAX( CASE t WHEN goal_t THEN s END ) AS goal_s , goal_t FROM (SELECT s , t , MIN( CASE WHEN s <> 2 THEN t END ) OVER( ORDER BY t ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) AS goal_t FROM t ) GROUP BY goal_t HAVING COUNT(goal_t) > 1 ORDER BY t ; ------------------------------------------------------------------------------ S T GOAL_S GOAL_T ----------- ----------- ----------- ----------- 2 2 3 3 2 4 3 6 2 7 0 8 3 record(s) selected. |
#5
| |||
| |||
|
|
WITH t(s,t) as ( VALUES (1,1), (2,2), (3,3), (2,4) , (2,5), (3,6), (2,7), (0,8) ) SELECT MIN(start_s) AS s , MIN(t) AS t , MAX(CASE t WHEN goal_t THEN s END ) AS goal_s , goal_t FROM (SELECT s , t , CASE WHEN s = 2 AND LAG(s) OVER(ORDER BY t) <> s THEN s END AS start_s |
#6
| |||
| |||
|
|
In case s is first in sequence (no predessors, like 1), LAG will result in null, and start_s will become null. I forgot that case. |
|
A generalization might be: * * ... AND coalesce(LAG(s) OVER(ORDER BY t),s+1) <> s THEN ... or: |
#7
| |||
| |||
|
|
In case s is first in sequence (no predessors, like 1), LAG will result in null, and start_s will become null. I forgot that case. A generalization might be: ... AND coalesce(LAG(s) OVER(ORDER BY t),s+1) <> s THEN ... or: ... AND LAG(s , 1 , s+1) OVER(ORDER BY t) <> s THEN ... |
#8
| |||
| |||
|
#9
| |||
| |||
|
|
Here is another simplified example: ------------------------------ Commands Entered ------------------------------ WITH t(s,t) as ( VALUES (1,1), (2,2), (3,3), (2,4) * * *, (2,5), (3,6), (2,7), (0,8) ) SELECT 2 * * *AS s * * *, MIN(t) AS t * * *, MAX( CASE t * * * * * * WHEN goal_t THEN s * * * * * * END * * * * * ) * AS goal_s * * *, goal_t *FROM *(SELECT s , t * * * * * * *, MIN( CASE WHEN s <> 2 THEN t END ) * * * * * * * * * OVER( ORDER BY t * * * * * * * * * * * * ROWS BETWEEN CURRENT ROW * * * * * * * * * * * * * * * * *AND UNBOUNDED FOLLOWING * * * * * * * * * * * ) AS goal_t * * * * *FROM *t * * * *) *GROUP BY * * * *goal_t *HAVING * * * *COUNT(goal_t) > 1 *ORDER BY * * * *t ; ---------------------------------------------------------------------------*--- S * * * * * T * * * * * GOAL_S * * *GOAL_T ----------- ----------- ----------- ----------- * * * * * 2 * * * * * 2 * * * * * 3 * * * * * 3 * * * * * 2 * * * * * 4 * * * * * 3 * * * * * 6 * * * * * 2 * * * * * 7 * * * * * 0 * * * * * 8 * 3 record(s) selected. This may be faster, because of avoidance of grouping. |
#10
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |