On Dec 17, 4:17 am, Rem-8 <[email protected]> wrote:
> Oh, I got this one on my own :)
>
> SQL> select target, sub_target,load,
> 2 case when target = prev_target and sub = prev_sub then
> load - lag(load) over
> (order by target, sub_target, ts) end incr_load
> 3 from
> 4 (select target, sub_target, ts, load, lag(target) over (order by
> target, sub_target, ts) prev_target, lag(sub) over (order by
> target, sub_target, ts) prev_sub
> 5 from sales_data);
>
> Thanks David for all. It was a great pleasure to work with you :)
You're welcome. But, I see some typos in your code; correcting those
produces a working query and the correct output:
SQL> select target, sub_target,load,
2 case when target = prev_target and sub_target = prev_sub
then load - lag(load) over (order by target, sub_target, ts) end
incr_load
3 from
4 (select target, sub_target, ts, load, lag(target) over (order by
target, sub_target, ts) prev_target, lag(sub_target) over (order by
target, sub_target, ts) prev_sub
5 from sales_data);
TARGET SUB_ LOAD INCR_LOAD
---------------- ---- ---------- ----------
192.168.1.1 1a 144
192.168.1.1 1a 147 3
192.168.1.1 1b 145
192.168.1.1 1b 150 5
192.168.1.1 2a 142
192.168.1.1 2a 142 0
192.168.1.1 2b 145
192.168.1.1 2b 147 2
192.168.1.2 6s 53
192.168.1.2 6s 66 13
192.168.1.2 6t 56
TARGET SUB_ LOAD INCR_LOAD
---------------- ---- ---------- ----------
192.168.1.2 6t 62 6
192.168.1.2 6u 53
192.168.1.2 6u 63 10
192.168.1.2 6v 58
192.168.1.2 6v 71 13
192.168.1.3 7a 22
192.168.1.3 7a 29 7
192.168.1.4 8a 34
192.168.1.4 8a 35 1
192.168.1.5 9a 11
192.168.1.5 9a 15 4
TARGET SUB_ LOAD INCR_LOAD
---------------- ---- ---------- ----------
192.168.1.6 9g 8
192.168.1.6 9g 9 1
192.168.1.7 9h 4
192.168.1.7 9h 5 1
192.168.1.8 9i 2
192.168.1.8 9i 7 5
28 rows selected.
SQL>
David Fitzjarrell
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Oracle PL/SQL" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Oracle-PLSQL?hl=en
-~----------~----~----~----~------~----~------~--~---