dbTalk Databases Forums  

help with sql query

comp.databases comp.databases


Discuss help with sql query in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rich G
 
Posts: n/a

Default help with sql query - 01-09-2009 , 09:20 AM






Can anyone help ?

Here's my query :
SELECT exe.OriginalName,exesess.StartTime,exesess.endtime

FROM Applications as app

JOIN Executables as exe ON exe.ApplicationID = app.ApplicationID

JOIN ExecutableSessions AS exesess ON exesess.ExecutableID =
exe.ExecutableID

JOIN ComputerSessions AS compsess ON compsess.ComputerSessionID =
exesess.ComputerSessionID

JOIN ComputerNames as cname ON cname.ComputerID = compsess.ComputerID


WHERE cname.Name = 'test2xp02'

AND cname.Domain = 'test2.local'

AND cname.Endtime IS NULL

AND app.Name = 'NONE'

and exesess.endtime is NULL

which produces :

EXPLORER.EXE, 2009-01-09 14:28:13.817, NULL
VPCMap.exe, 2009-01-09 14:28:13.910, NULL
svchost.exe, 2009-01-09 14:28:13.973, NULL
msmsgs.exe, 2009-01-09 14:28:14.317, NULL
svchost.exe,2009-01-09 14:28:14.850,NULL
svchost.exe, 2009-01-09 14:28:15.240,NULL

This is ok. But I want to refine it to produce one row for duplicate
'originalname'('svchost' in the above). The 'originalname' to be returned in
the case of a duplicate would be the one with the earliest start time.

So I want something list this :

EXPLORER.EXE, 2009-01-09 14:28:13.817, NULL
VPCMap.exe, 2009-01-09 14:28:13.910, NULL
svchost.exe, 2009-01-09 14:28:13.973, NULL
msmsgs.exe, 2009-01-09 14:28:14.317, NULL

I have been pulling my hair our with this one trying subqueries and TOP but
I'm now confused.

Can anyone offer any advice ?

Thanks in advance

Richard



Reply With Quote
  #2  
Old   
Ed Prochak
 
Posts: n/a

Default Re: help with sql query - 01-09-2009 , 02:33 PM






On Jan 9, 10:20*am, "Rich G" <nos... (AT) nospam (DOT) com> wrote:
Quote:
Can anyone help ?
[]

*which produces :

EXPLORER.EXE, 2009-01-09 14:28:13.817, NULL
VPCMap.exe, 2009-01-09 14:28:13.910, NULL
svchost.exe, 2009-01-09 14:28:13.973, NULL
msmsgs.exe, 2009-01-09 14:28:14.317, NULL
svchost.exe,2009-01-09 14:28:14.850,NULL
svchost.exe, 2009-01-09 14:28:15.240,NULL

This is ok. But I want to refine it to produce one row for duplicate
'originalname'('svchost' in the above). The 'originalname' to be returnedin
the case of a duplicate would be the one with the earliest start time.

So I want something list this :

EXPLORER.EXE, 2009-01-09 14:28:13.817, NULL
VPCMap.exe, 2009-01-09 14:28:13.910, NULL
svchost.exe, 2009-01-09 14:28:13.973, NULL
msmsgs.exe, 2009-01-09 14:28:14.317, NULL

I have been pulling my hair our with this one trying subqueries and TOP but
I'm now confused.

Can anyone offer any advice ?

Thanks in advance

Richard
Is it significant that the endtime values are all NULL?? If so, why
bother? There must be something you aren't telling us.

Based on the example data shown, it looks like a GROUP BY
and MIN(exesess.StartTime)
should work.
HTH
ed


Reply With Quote
  #3  
Old   
Rich G
 
Posts: n/a

Default Re: help with sql query - 01-09-2009 , 07:34 PM




"Ed Prochak" <edprochak (AT) gmail (DOT) com> wrote

On Jan 9, 10:20 am, "Rich G" <nos... (AT) nospam (DOT) com> wrote:
Quote:
Can anyone help ?
[]

which produces :

EXPLORER.EXE, 2009-01-09 14:28:13.817, NULL
VPCMap.exe, 2009-01-09 14:28:13.910, NULL
svchost.exe, 2009-01-09 14:28:13.973, NULL
msmsgs.exe, 2009-01-09 14:28:14.317, NULL
svchost.exe,2009-01-09 14:28:14.850,NULL
svchost.exe, 2009-01-09 14:28:15.240,NULL

This is ok. But I want to refine it to produce one row for duplicate
'originalname'('svchost' in the above). The 'originalname' to be returned
in
the case of a duplicate would be the one with the earliest start time.

So I want something list this :

EXPLORER.EXE, 2009-01-09 14:28:13.817, NULL
VPCMap.exe, 2009-01-09 14:28:13.910, NULL
svchost.exe, 2009-01-09 14:28:13.973, NULL
msmsgs.exe, 2009-01-09 14:28:14.317, NULL

I have been pulling my hair our with this one trying subqueries and TOP
but
I'm now confused.

Can anyone offer any advice ?

Thanks in advance

Richard
Is it significant that the endtime values are all NULL?? If so, why
bother? There must be something you aren't telling us.

Based on the example data shown, it looks like a GROUP BY
and MIN(exesess.StartTime)
should work.
HTH
----------------------

Thanks for the reply.

The endtime values are NULL because the column represents the endtime for an
executable program which is currently executing. This is updated when the
executable finishes executing. You are right they do represent and unknown
endtime above.

I have managed to produce this query which seems to solve my problem.

Any comments/critique is welcome :

SELECT
exe.OriginalName,exe.CompanyName,exe.FileVersion,e xesess.StartTime,exesess.endtime

FROM Applications AS app

JOIN Executables AS exe ON exe.ApplicationID = app.ApplicationID

JOIN ExecutableSessions AS exesess ON exesess.ExecutableID =
exe.ExecutableID

JOIN ComputerSessions AS compsess ON compsess.ComputerSessionID =
exesess.ComputerSessionID

JOIN ComputerNames AS cname ON cname.ComputerID = compsess.ComputerID

WHERE cname.Name = @ComputerName

AND cname.Domain = @DomainName

AND cname.Endtime IS NULL

AND app.Name = 'NONE'

AND exesess.endtime IS NULL

AND exesess.StartTime = (SELECT TOP 1 exesess2.StartTime FROM

ExecutableSessions AS exesess2

JOIN ComputerSessions AS compsess2 ON compsess2.ComputerSessionID =
exesess2.ComputerSessionID

JOIN ComputerNames AS cname2 ON cname2.ComputerID = compsess2.ComputerID

WHERE exesess2.ExecutableID = exesess.ExecutableID

AND compsess2.EndTime IS NULL

AND cname2.EndTime IS NULL

AND cname2.Name = @ComputerName

AND cname2.Domain = @DomainName

ORDER BY exesess.StartTime)

Thanks Richard





Reply With Quote
  #4  
Old   
Hans Chr Sundsfjord
 
Posts: n/a

Default Re: help with sql query - 01-10-2009 , 05:10 AM




"Rich G" <nospam (AT) nospam (DOT) com> skrev i nyhetsmeldingen:
2PqdnREQWIWXZvrUnZ2dnUVZ8uGdnZ2d (AT...e (DOT) net.uk ...
Quote:
"Ed Prochak" <edprochak (AT) gmail (DOT) com> wrote in message
news:2441eac6-2d69-4423-be1a-8d3f7f6360ca (AT) t3g2000yqa (DOT) googlegroups.com...
On Jan 9, 10:20 am, "Rich G" <nos... (AT) nospam (DOT) com> wrote:
Can anyone help ?
[]

which produces :

EXPLORER.EXE, 2009-01-09 14:28:13.817, NULL
VPCMap.exe, 2009-01-09 14:28:13.910, NULL
svchost.exe, 2009-01-09 14:28:13.973, NULL
msmsgs.exe, 2009-01-09 14:28:14.317, NULL
svchost.exe,2009-01-09 14:28:14.850,NULL
svchost.exe, 2009-01-09 14:28:15.240,NULL

This is ok. But I want to refine it to produce one row for duplicate
'originalname'('svchost' in the above). The 'originalname' to be returned
in
the case of a duplicate would be the one with the earliest start time.

So I want something list this :

EXPLORER.EXE, 2009-01-09 14:28:13.817, NULL
VPCMap.exe, 2009-01-09 14:28:13.910, NULL
svchost.exe, 2009-01-09 14:28:13.973, NULL
msmsgs.exe, 2009-01-09 14:28:14.317, NULL

I have been pulling my hair our with this one trying subqueries and TOP
but
I'm now confused.

Can anyone offer any advice ?

Thanks in advance

Richard

Is it significant that the endtime values are all NULL?? If so, why
bother? There must be something you aren't telling us.

Based on the example data shown, it looks like a GROUP BY
and MIN(exesess.StartTime)
should work.
HTH
----------------------

Thanks for the reply.

The endtime values are NULL because the column represents the endtime for
an executable program which is currently executing. This is updated when
the executable finishes executing. You are right they do represent and
unknown endtime above.

I have managed to produce this query which seems to solve my problem.

Any comments/critique is welcome :

SELECT
exe.OriginalName,exe.CompanyName,exe.FileVersion,e xesess.StartTime,exesess.endtime

FROM Applications AS app

JOIN Executables AS exe ON exe.ApplicationID = app.ApplicationID

JOIN ExecutableSessions AS exesess ON exesess.ExecutableID =
exe.ExecutableID

JOIN ComputerSessions AS compsess ON compsess.ComputerSessionID =
exesess.ComputerSessionID

JOIN ComputerNames AS cname ON cname.ComputerID = compsess.ComputerID

WHERE cname.Name = @ComputerName

AND cname.Domain = @DomainName

AND cname.Endtime IS NULL

AND app.Name = 'NONE'

AND exesess.endtime IS NULL

AND exesess.StartTime = (SELECT TOP 1 exesess2.StartTime FROM

ExecutableSessions AS exesess2

JOIN ComputerSessions AS compsess2 ON compsess2.ComputerSessionID =
exesess2.ComputerSessionID

JOIN ComputerNames AS cname2 ON cname2.ComputerID = compsess2.ComputerID

WHERE exesess2.ExecutableID = exesess.ExecutableID

AND compsess2.EndTime IS NULL

AND cname2.EndTime IS NULL

AND cname2.Name = @ComputerName

AND cname2.Domain = @DomainName

ORDER BY exesess.StartTime)

Thanks Richard



Hi,

Instead of joining over againg through a subquery, I would think of using
these

....
GROUP BY , , ,
HAVING MIN(exesess.StartTime) = exesess.StartTime
;

or alternatively use a PARTITION BY and then QUALIFY, if you've got support
for this in your RDBMS:

....
QUALIFY ROW_NUMBER() OVER( PARTITION BY , , , ORDER BY exesess.StartTime
ASC)=1
;

I would guess that the execution plan will be more efficient than
(correlated) subqueries.

Good Luck:-)
Hans Sundsfjord

---
Hans Chr Sundsfjord
hans.c.sundsfjord (AT) online (DOT) no









Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.