[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL show Row with MIN datetime

Posted on 2011-09-27
8
Medium Priority
?
375 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

873 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