Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Advice with Multiple Joins / Selects

Author  Topic 

fezz
Starting Member

12 Posts

Posted - 2009-03-09 : 12:28:07
Afternoon All,

I have the query below which takes waaaaaaay to long to run, if anyone can see a better way of doing it i would appreciate some advice :)

SELECT     name, owneridname AS AccountManager, new_telemarketingownername AS TelemarketingOwner, accountid, new_autonumbertext,
(SELECT TOP (1) fpc1.actualend
FROM FilteredPhoneCall AS fpc1 INNER JOIN
FilteredActivityParty AS fap1 ON fpc1.activityid = fap1.activityid INNER JOIN
FilteredContact AS fc1 ON fap1.partyid = fc1.contactid INNER JOIN
FilteredAccount AS fa1 ON fc1.accountid = fa1.accountid
WHERE (fap1.participationtypemaskname = N'To Recipient') AND (fa.accountid = fc1.accountid) AND (fpc1.statecode = '1')
ORDER BY fpc1.actualend DESC) AS LastCallDate,
(SELECT TOP (1) fpc1.subject
FROM FilteredPhoneCall AS fpc1 INNER JOIN
FilteredActivityParty AS fap1 ON fpc1.activityid = fap1.activityid INNER JOIN
FilteredContact AS fc1 ON fap1.partyid = fc1.contactid INNER JOIN
FilteredAccount AS fa1 ON fc1.accountid = fa1.accountid
WHERE (fap1.participationtypemaskname = N'To Recipient') AND (fa.accountid = fc1.accountid) AND (fpc1.statecode = '1')
ORDER BY fpc1.actualend DESC) AS LastPhoneCallSubject,
(SELECT TOP (1) fpc1.owneridname
FROM FilteredPhoneCall AS fpc1 INNER JOIN
FilteredActivityParty AS fap1 ON fpc1.activityid = fap1.activityid INNER JOIN
FilteredContact AS fc1 ON fap1.partyid = fc1.contactid INNER JOIN
FilteredAccount AS fa1 ON fc1.accountid = fa1.accountid
WHERE (fap1.participationtypemaskname = N'To Recipient') AND (fa.accountid = fc1.accountid) AND (fpc1.statecode = '1')
ORDER BY fpc1.actualend DESC) AS LastPhoneCallOwner,
(SELECT TOP (1) fpc1.owneridname
FROM FilteredPhoneCall AS fpc1 INNER JOIN
FilteredActivityParty AS fap1 ON fpc1.activityid = fap1.activityid INNER JOIN
FilteredContact AS fc1 ON fap1.partyid = fc1.contactid INNER JOIN
FilteredAccount AS fa1 ON fc1.accountid = fa1.accountid
WHERE (fap1.participationtypemaskname = N'To Recipient') AND (fa.accountid = fc1.accountid) AND (fpc1.statecode = '0')
ORDER BY fpc1.scheduledend) AS NextPhoneCallOwner,
(SELECT TOP (1) fpc1.scheduledend
FROM FilteredPhoneCall AS fpc1 INNER JOIN
FilteredActivityParty AS fap1 ON fpc1.activityid = fap1.activityid INNER JOIN
FilteredContact AS fc1 ON fap1.partyid = fc1.contactid INNER JOIN
FilteredAccount AS fa1 ON fc1.accountid = fa1.accountid
WHERE (fap1.participationtypemaskname = N'To Recipient') AND (fa.accountid = fc1.accountid) AND (fpc1.statecode = '0')
ORDER BY fpc1.scheduledend) AS NextCallDate
FROM FilteredAccount AS fa
WHERE (statuscode = '1')


Pete

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-03-09 : 13:37:52
Try this structure.

SELECT [name]
,owneridname AS AccountManager
,new_telemarketingownername AS TelemarketingOwner
,accountid
,new_autonumbertext
,oa1.LastCallDate
,oa1.subject
,oa1.owneridname
,oa2.owneridname
,oa2.scheduledend

FROM FilteredAccount AS fa

OUTER APPLY (
select top 1
fpc1.actualend AS LastCallDate
,fpc1.subject
,fpc1.owneridname
FROM FilteredPhoneCall AS fpc1
INNER JOIN FilteredActivityParty AS fap1 ON fap1.activityid = fpc1.activityid
INNER JOIN FilteredContact AS fc1 ON fc1.contactid = fap1.partyid
where fc1.accountid = fa.accountid
and fap1.participationtypemaskname = N'To Recipient'
AND fpc1.statecode = '1'
order by fpc1.actualend DESC
) oa1

OUTER APPLY (
select top 1
fpc1.owneridname
,fpc1.scheduledend
FROM FilteredPhoneCall AS fpc1
INNER JOIN FilteredActivityParty AS fap1 ON fap1.activityid = fpc1.activityid
INNER JOIN FilteredContact AS fc1 ON fc1.contactid = fap1.partyid
where fc1.accountid = fa.accountid
and fap1.participationtypemaskname = N'To Recipient'
AND fpc1.statecode = '0'
order by fpc1.scheduledend
) oa2

WHERE fa.statuscode = '1'


Be One with the Optimizer
TG
Go to Top of Page

fezz
Starting Member

12 Posts

Posted - 2009-03-10 : 10:31:44
Thanks TG, My sql knowledge is growing each day and i can see that type of query coming in very useful.




Pete
Go to Top of Page
   

- Advertisement -