Solved

SQL show Row with MIN datetime

Posted on 2011-09-27
8
356 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
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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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 500 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

679 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