Solved

SQL show Row with MIN datetime

Posted on 2011-09-27
8
325 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
Comment Utility
do you have a primary key there?
0
 
LVL 42

Expert Comment

by:dqmq
Comment Utility
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
Comment Utility
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
 

Author Comment

by:TechMonster
Comment Utility
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

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

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
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.
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now