Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

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
0
globalwm2
Asked:
globalwm2
  • 2
1 Solution
 
slightwv (䄆 Netminder) 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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now