Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

find unique value from a three way join

Posted on 2008-10-29
5
Medium Priority
?
527 Views
Last Modified: 2012-05-05
Trying to capture processes taking io and need to select distiinct spid from the query.  Was trying to select it into a temp table, but not allowed due to 'NULL' values.  Using distinct also isn't helping.  I must be doing my join incorrectly but unsure how to proceed... it has to be something elementary.

     select convert (char(19), getdate()),
                convert (char(15), b.name) as login,
                convert (char(15), c.name) as dbname,
                convert  (char(4), a.spid) as spid,
                                             a.status,
                                             acmd,
                                             a.physical_io,
               convert  (char(5), a.cpu) as cpu,
               convert  (char(5), a.blocked) as block,
               convert  (char(5), a.time_blocked) as time

    from master..sysprocesses   a,
            master..syslogins           b,
            master..sysdatabases   c

    where physical_io > 0
0
Comment
Question by:sanate
[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
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:RPCIT
ID: 22833169
The SQL you wrote has some basic problems that I see, for instance you don't have any relations defined, but this could just be because I use MSSQL 2000 and 2005.  

But regardless, you should be able to wrap the whole thing (assuming your happy with the results) with
   SELECT DISTINCT spid FROM ( YOUR_SQL_HERE ) subQueryAlias

hope this helps.
SELECT DISTINCT spid FROM (
	       select convert (char(19) , getdate() )AS _now,
                convert (char(15), b.name) as login,
                convert (char(15), c.name) as dbname,
                convert  (char(4), a.spid) as spid,
                                             a.status,
                                             a.cmd,
                                             a.physical_io,
               convert  (char(5), a.cpu) as cpu,
               convert  (char(5), a.blocked) as block--,
               --convert  (char(5), a.time_blocked) as time
 
    from master..sysprocesses   a,
            master..syslogins           b,
            master..sysdatabases   c
 
    where physical_io > 0) sub

Open in new window

0
 

Author Comment

by:sanate
ID: 22833391
Thanks for looking into this.  But we've been having some issues tracking logins giving the databases angst.  This solution does indeed work for isolating unique spids... but also need to capture the other columns in the select statement.  By the unique statement I'm getting a row returned for every database... so its returning in this case 15 rows for every spid.
0
 
LVL 6

Expert Comment

by:RPCIT
ID: 22833711
you just need to add the fields that you want to see, and then some aggregate function for the others.  If you want all the fields, then distict will only work where all the fields are identical.

SELECT DISTICT spid, MAX(Login) as Login FROM (.....) alias GROUP BY spid

would do a rollup, and only give you spids, but you would lost detail on the login field.
0
 
LVL 19

Accepted Solution

by:
grant300 earned 500 total points
ID: 22833896
The problem is you are not specifying the join SARGs in the WHERE clause so you are getting a Cartesian product; A x B x C rows total.  You have to join the suid in the sysprocesses table to the suid in syslogins and the dbid in sysprocesses to the dbid in sysdatabases.

I personally prefer to use the ANSI SQL syntax for joins as they are less ambiguous and easier to read.  See the snippet.

Regards,
Bill

  select convert (char(19), getdate()),
         convert (char(15), b.name) as login,
         convert (char(15), c.name) as dbname,
         convert  (char(4), a.spid) as spid,
         a.status,
         a.cmd,
         a.physical_io,
         convert  (char(5), a.cpu) as cpu,
         convert  (char(5), a.blocked) as block,
         convert  (char(5), a.time_blocked) as time
    from master..sysprocesses   a
    join master..syslogins      b
      on b.suid = a.suid
    join master..sysdatabases   c
      on c.dbid = a.dbid
   where physical_io > 0

Open in new window

0
 

Author Closing Comment

by:sanate
ID: 31511270
Thanks much, don't know why I was drawing a blank on that.  Must have wasted half of yesterday on it.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

688 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