SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 subqueries
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tinac99
Starting Member

1 Posts

Posted - 12/17/2012 :  13:33:23  Show Profile  Reply with Quote
Hi,

I'm supposed to display a project, with its list of suspects and its list of victims.

I'm constructing a sql with possibly 2 subqueries:
select pj.projectno,
pj.OpenCaseFlag,
pj.DateTimeReported,
suspects = (select p.lastname,p.firstname from suspect s inner join person p on s.personid = p.personid and s.projectno = pj.projectno)
suspects = (select p.lastname,p.firstname from victim v inner join person p on v.personid = p.personid and v.projectno = pj.projectno)
from projects pj

Can anyone give an insight on the performance of this query?
I thought of doing all outer joins:

select
pj.projectno,
pj.OpenCaseFlag,
pj.DateTimeReported,
v.victimId,
pv.Lastname,
pv.Firstname,
s.suspectId,
ps.LastName,
ps.FirstName,
from dbo.Project pj
left outer join (dbo.Victim v inner join dbo.person pv on v.personid = pv.personid) on t.ProjectNo = v.ProjectNo
left outer join (dbo.Suspect s inner join dbo.person ps on s.personid = ps.personid) on t.ProjectNo = s.ProjectNo

But the all suspects are listed on every victim instead of the project, which makes the count wrong. Also, it will have an error on paging(page size) and when I click prev/next on the result set on the client end.

If anybody has a more effiecient way of doing this, please let me know.

Thanks,

Christina

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/17/2012 :  13:48:46  Show Profile  Reply with Quote
Post you query with sample and expected output so that people will help you.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000