Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LAG OVER - not returning expected results when without a WHERE

Posted on 2011-03-04
3
Medium Priority
?
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 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 77

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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.  …
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

721 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