Solved

LAG OVER - not returning expected results when without a WHERE

Posted on 2011-03-04
3
408 Views
Last Modified: 2012-06-21
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
Comment
Question by:globalwm2
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 125 total points
ID: 35038924
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
 

Author Closing Comment

by:globalwm2
ID: 35038967
Exactly - that was what I needed:

LAG(q1.ordered_date,1) OVER (PARTITION by EMAIL ORDER BY q1.ordered_date) prev_order_date
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35038999
Glad to help.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question