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, 01:00 PM
InsureDesign InsureDesign is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 44 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: 9 h 57 m 31 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 01:24 PM.

Reply With Quote
  #2  
Old September 23rd, 2009, 04: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, 05:27 AM
sakthi.tnj sakthi.tnj is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 23 sakthi.tnj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 48 m 35 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!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

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





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