Oracle - Count oads with duplicated stops in sequence no2

Hi,
i was asked to pull the same Origin Destination and count of the same table from my previous topic but only when we have duplicates DL that follows in sequence.

Previous topic:
http://www.experts-exchange.com/questions/28735122/Oracle-Count-oads-with-duplicated-stops-in-sequence.html

Ex:
00001 = PK
00002 = DL
00002 = DL
00003 = DL

Since after the PK, 00002 shows 2 times in a row, i need to count it.

If  i see the below scenario, even if i have 2 times 00002 but not one after the other, that would not count.
00001 = PK
00002 = DL
00003 = DL
00002 = DL

The format result would be the same as the previous Query accept that now, it will only count when same DL shows more then once onne after the other, so consecutive.

What do i need to adjust in the query?

This is an example of the table again with the result that i would like to get from the query.
Duplicate DL
Thanks again for your help.
Copy-of-sql_load_stop_no3.xlsx
LVL 11
Wilder1626Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
try this


 SELECT COUNT(DISTINCT dl.load_id) bol_count, pk.location_id from_orgin, dl.location_id to_destination
    FROM (SELECT x.*, MIN(recent_pk) OVER (PARTITION BY load_id, location_id) first_pk
            FROM (SELECT tab1.*,
                         LAST_VALUE(CASE WHEN stop_type = 'PK' THEN stop_num END IGNORE NULLS)
                         OVER(PARTITION BY load_id ORDER BY stop_num ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
                             recent_pk,
                         LAG(location_id) over(partition by load_id order by stop_num) prev_loc
                         
                    FROM tab1) x
           WHERE stop_type = 'DL' AND location_id = prev_loc) dl,
         (SELECT *
            FROM tab1
           WHERE stop_type = 'PK') pk
   WHERE dl.load_id = pk.load_id AND dl.first_pk = pk.stop_num
GROUP BY pk.location_id, dl.location_id;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wilder1626Author Commented:
That looks very good. Thanks again for your help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.