LAG OVER - not returning expected results when without a WHERE

I have this statement which returns expected results for a single email:

SELECT email, ordered_date,
                  LAG(ordered_date,1) OVER (ORDER BY ordered_date) prev_order_date
                  FROM APPS.XXOC_DISC_SALES_MASTER_MV
                  WHERE EMAIL = 'TEDNUGENT@SNET.NET'
                  GROUP BY email, ordered_date
                  order by email, ordered_date

Returns  

But when I remove the WHERE clause and run for all, the lag does not group as expected.

Any ideas?   RESULTSET
globalwm2Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
slightwv (䄆 Netminder)Connect With a Mentor Commented:
My guess is when you remove the where clause it is doing what it is supposed to do.

Are you wanting to generate the LAG based on email over the entire report?

if so, add a partition:
...
OVER (PARTITION by EMAIL ORDER BY ordered_date)
...
0
 
globalwm2Author Commented:
Exactly - that was what I needed:

LAG(q1.ordered_date,1) OVER (PARTITION by EMAIL ORDER BY q1.ordered_date) prev_order_date
0
 
slightwv (䄆 Netminder) Commented:
Glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.