SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old July 16th, 2009, 12:00 PM
InsureDesign InsureDesign is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 46 InsureDesign User rank is Private First Class (20 - 50 Reputation Level)InsureDesign User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 10 h 14 m 26 sec
Reputation Power: 6
Lag/Lead not the full solution.

Here is what I'm trying to do, I have a table that has, among other things, the following columns:

EVENT_ID,
AUDIT_ID,
EVENT_DATE

Here is some data
Code:
EVENT_ID       AUDIT_ID      EVENT_DATE
100143             153            07/15/2009
100142             153            07/15/2009
100132             153            07/01/2009
100102             153            06/23/2009
100152             326            07/16/2009
100137             326            07/03/2009
100011             326            06/06/2009
100141             257            07/15/2009
100120             257            06/28/2009
100150             186            07/16/2009
100148             186            07/16/2009
100123             186            06/28/2009


What I am trying to accomplish is the following:

- Get all of the events that happened either today or yesterday. This is the easy part.
- Take the AUDIT_ID of those events and go back to the next event that did not happen today or yesterday and retreive the date that it happened.
- Calculate the days between these events. This is also easy.

So basically in the end I am looking for the data to be similar to this:

Code:
AUDIT_ID      EVENT_DATE            PREV_DATE       DAYS
153               07/15/2009             07/01/2009         15    
326               07/16/2009             07/03/2009         14
257               07/15/2009             06/28/2009         18
186               07/16/2009             06/28/2009         17


I know I can use the Lag/Lead functions, but I can't figure out an efficent and accurate way to use them.

Any pointers/functions/code would be greatly appriciated. Incase you were wondering, I am using oracle.

Last edited by InsureDesign : July 16th, 2009 at 12:24 PM.

Reply With Quote
  #2  
Old September 23rd, 2009, 03:04 PM
gregory.owen@hp gregory.owen@hp is offline
Maniac
ASP Free Novice (500 - 999 posts)
 
Join Date: Sep 2003
Location: Sweet Home, Oregon
Posts: 598 gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level)gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level)gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level)gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level)gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level)gregory.owen@hp User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 14 h 46 m 46 sec
Reputation Power: 37
I haven't worked with Oracle since 9i, so I don't know if this will work or not, but it works in ms sql server:

Code:
select distinct s.audit_id, s.Event_Date, d.Prev_date, DATEDIFF(d, d.Prev_date, s.Event_Date) as days
from events s
join
(select audit_id, MAX(event_date) as prev_date from events
	WHERE event_date < dateadd(d,-1,'2009-07-16')
	GROUP BY audit_id) d
on s.audit_id = d.Audit_id 
WHERE s.Event_Date >= dateadd(d,-1,'2009-07-16')

Reply With Quote
  #3  
Old October 12th, 2009, 04:27 AM
sakthi.tnj sakthi.tnj is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 24 sakthi.tnj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 45 m 13 sec
Reputation Power: 0
Re: Lag/Leap not the full solution

Hi,

I hope that the below SQL query will be very useful for you....

If you consider the event date as a current date, use the following query
Code:
Select 
		a.audit_id as Audit_id, 
		convert(char,a.event_date,101) as Event_date,
		convert(char,b.event_date,101) as Previousdate,
	datediff(dd,convert(char,b.event_date,101),convert  (char,a.event_date,101)) as Days
from 
		test2 a INNER JOIN
		(Select audit_id , event_date, rno from(select audit_id, 
event_date,
row_number()over(partition by audit_id order by event_date desc)as rno from test2)tbl 
		where rno =2)b on a.audit_id = b.audit_id
where 
		(convert(char,a.event_date,101) = convert(char,getdate(),101) or
		datepart(dd,convert(char,a.event_date,101)) = (datepart(dd,convert(char,getdate(),101))-1))



If you consider the event date as max date, use the following query.

Code:

Select 
		a.audit_id as Audit_id, 
		max(convert(char,a.event_date,101))as Event_date, 
		convert(char,b.event_date,101) as Previousdate, 
		datediff(dd, convert(char,b.event_date,101),max (convert(char ,a.event_date,101)))as Days  
from 
		test2 a,
		(select 
                     event_date,
row_number()over(partition by audit_id order by event_date desc)as rno from test2)b
where 
		b.rno = 2 
group by
		 audit_id, 
		b.event_date


Note: Test2 is a table name

Thanks & Regards
Sakthimeenakshi.S
------------------------------------------------

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Lag/Leap not the full solution.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 7 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek