Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL show Row with MIN datetime

Posted on 2011-09-27
8
Medium Priority
?
373 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

609 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