?
Solved

SQL show Row with MIN datetime

Posted on 2011-09-27
8
Medium Priority
?
366 Views
Last Modified: 2012-06-27
Dataset filtered by Window title below.  I have to display the rows based upon date and window title. Win1 or Win2 determines a point of entry but they are sometimes both used and I only want to display the one which has the min time within 10min. So, for OPID 5428 I don't want to display rows:
9/26/11 3:36 AM      5428      Win2
9/26/11 3:37 AM      5428      Win2


Data Sample::
Date	         OPID	Window title
9/26/11 7:51 AM	8822	Win1
9/26/11 3:36 AM	5428	Win1
9/26/11 3:36 AM	5428	Win2
9/26/11 3:37 AM	5428	Win2
9/26/11 6:30 AM	5428	Win1
9/26/11 6:54 AM	5428	Win1
9/27/11 10:01 AM	8822	Win1
9/27/11 11:35 AM	5428	Win2

SHOULD RETURN:

Date	         OPID	Window title
9/26/11 7:51 AM	8822	Win1
9/26/11 3:36 AM	5428	Win1
9/26/11 6:30 AM	5428	Win1
9/26/11 6:54 AM	5428	Win1
9/27/11 10:01 AM	8822	Win1
9/27/11 11:35 AM	5428	Win2

Open in new window

0
Comment
Question by:TechMonster
[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
8 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 36710064
do you have a primary key there?
0
 
LVL 42

Expert Comment

by:dqmq
ID: 36710123
Please clarify your requirements.  Seems to me that win1 and win2 both have the same "min" time. What is the rational for choosing win1 over win2?  Also, it seems to me that some of the win1 rows that you want to see DO NOT have the min time.  

Also, what do you mean by "within 10 minutes"
0
 
LVL 18

Expert Comment

by:deighton
ID: 36710174
is it within ten minutes of a row you have included?

so

3:36
3:45
3:46
3:47

include 3:36
exclude 3:45 & 3:46
but then include 3:47

or would you exclude 3:47?

is that the logic?
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:TechMonster
ID: 36710231
no primary key.  
Win and win2 are just methods of getting into an account.  I just want the first known time that they accessed it.  So if two times are the same,,just distinct.  
For the same OPID I don't want to show any row with the same account number (a column I didn't include in my example.  The data sample is all from one account) until at least 10min has elapsed.   So if OPID 5428 access a account at 1:00 then at 1:01 OPID 8822 access it,,I want to show both rows. If OPID 5428 accessed it at 1:00 then also at 1:01 then I only want to show the row with the 1:00 access time.    

Hope this helps...thanks for looking into this.
0
 

Author Comment

by:TechMonster
ID: 36710240
I am also looking into cursors at the moment...seeing if I need to go row by row to get that distinction
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36710535
please see this article to know your (non-cursor) options:
http://www.experts-exchange.com/A_3203.html
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36711162
like this

select accountnumber,opid,[window title],[date],1 as lvl
   into #temp
from (
select accountnumber,opid,[window title],[date]
   ,row_number() over (partition by accountnumber order by [date]) as rn
from yourtable
) as x
where rn=1

declare @lvl int,@rows int
select @lvl=2,@rows=-1

while @rows <> 0
begin
  insert into #temp
select accountnumber,opid,[window title],[date],@lvl+1
   into #temp
from (
select a.accountnumber,a.opid,a.[window title],a.[date]
   ,row_number() over (partition by a.accountnumber order by a.[date]) as rn
from yourtable as a
inner join #temp as b
  on a.accountnumber=b.accountnumber
 and a.[date] > dateadd(minute,10,b.[date])
where b.lvl=@lvl
) as x
where rn=1
select @rows=@@rowcount,@lvl=@lvl+1
end

select accountnumber,[date],opid,[window title]
from #temp
order by 1,2
0
 
LVL 18

Accepted Solution

by:
lludden earned 2000 total points
ID: 36711446
This works:

DECLARE @T1 TABLE (Date datetime, OPID int, WinTitle varchar(5))
INSERT INTO @T1 ([@T1].[Date],[@T1].OPID,[@T1].WinTitle)
VALUES ('9/26/11 7:51 AM',	8822,	'Win1'),
('9/26/11 3:36 AM',	5428,	'Win1'),
('9/26/11 3:36 AM',	5428,	'Win2'),
('9/26/11 3:37 AM',	5428,	'Win2'),
('9/26/11 6:30 AM',	5428,	'Win1'),
('9/26/11 6:54 AM',	5428,	'Win1'),
('9/27/11 10:01 AM',	8822,	'Win1'),
('9/27/11 11:35 AM',	5428,	'Win2')

;WITH cteNewest AS ( --First, find the newest entry for each item in the table
SELECT T1.* FROM @T1 T1
LEFT OUTER JOIN @T1 T2 ON T1.WinTitle = T2.WinTitle
AND T2.OPID = T1.OPID
AND T2.[Date] < T1.[Date]
WHERE T2.[Date] IS NULL
)
SELECT DISTINCT T1.* FROM @T1 T1 --Now, we do join to the original table with the latest, and make sure that the times are not withing 10 min
LEFT OUTER JOIN cteNewest ON T1.OPID = cteNewest.OPID AND T1.WinTitle = cteNewest.WinTitle
WHERE (T1.[Date] > dateadd(n,10,cteNewest.[Date]) 
OR T1.[Date] = cteNewest.[Date])

Open in new window




0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

770 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