New to SQL how do I get both contactsid's from contacts and tasks using a left outter join?
I wrote this report to display how long it takes a user to finish their first task. currently it will only display contacts that have tasks. I need it to display both contacts with and without tasks any ideas?
SELECT c.MarketSource AS [Market Source],
Coalesce(u.FirstName + ' ', ' ') + Coalesce(u.LastName, ' ') AS [Producer],
Coalesce(c.FirstName + ' ', ' ') + Coalesce(c.MiddleName + ' ', ' ') + Coalesce(c.LastName, ' ') AS [Lead Name],
t.TaskType AS [Task],
Coalesce (t.workflow_resultchosen_label, t.result, ' ') AS [Task Result],
Dateadd(HOUR, @utcoffset, c.createdon) AS [Received On],
Dateadd(HOUR, @utcoffset, ( MIN (t.completed) )) 开发者_C百科 AS [Completed],
(SELECT COUNT(*)
FROM Quotes q WITH (NOLOCK)
WHERE SaleID IS NULL
AND c.ContactID = q.ContactID) AS [Pending Quotes],
(SELECT COUNT(*)
FROM Sales s WITH (NOLOCK)
WHERE s.Client_ID = c.ContactID) AS [Total Sales]
FROM Tasks t WITH (NOLOCK)
LEFT JOIN Users U WITH (NOLOCK)
ON u.UserID = t.UserID
LEFT JOIN Contacts C WITH (NOLOCK)
ON c.ContactID = t.TargetID
WHERE iLeadPartnerID IS NOT NULL
AND ( c.CreatedOn BETWEEN @startDate AND @endDate )
AND MarketSource IS NOT NULL
AND c.WorkgroupId = @workgroupID
AND Len(c.MarketSource) > 0
AND t.TaskType <> '[=] Auto Email'
GROUP BY c.MarketSource,
u.LastName,
u.FirstName,
c.ContactId,
c.LastName,
c.FirstName,
c.MiddleName,
c.CreatedOn,
t.TaskType,
t.Result,
t.Workflow_ResultChosen_Label
ORDER BY c.LastName,
c.MiddleName,
c.FirstName
You need to use a Left Join from Contacts to Tasks (i.e., show all contacts and any matching Tasks). In addition, you need to move the criteria t.TaskType <> '[=] Auto Email'
to the ON clause or change the Where clause filter to be:
(t.TaskType <> '[=] Auto Email' Or t.TaskType Is Null)
.
Otherwise, your Left Join to Tasks will effectively become an Inner Join.
SELECT c.MarketSource AS [Market Source]
, Coalesce(u.FirstName + ' ', ' ') + Coalesce(u.LastName, ' ') AS [Producer]
, Coalesce(c.FirstName + ' ', ' ') + Coalesce(c.MiddleName + ' ', ' ') + Coalesce(c.LastName, ' ') AS [Lead Name]
, t.TaskType AS [Task], Coalesce (t.workflow_resultchosen_label, t.result, ' ') AS [Task Result]
, Dateadd(HOUR, @utcoffset, c.createdon) AS [Received On]
, Dateadd(HOUR, @utcoffset, ( MIN (t.completed) )) AS [Completed]
, (SELECT COUNT() FROM Quotes q WITH (NOLOCK) WHERE SaleID IS NULL AND c.ContactID = q.ContactID) AS [Pending Quotes]
, (SELECT COUNT() FROM Sales s WITH (NOLOCK) WHERE s.Client_ID = c.ContactID) AS [Total Sales]
From Contacts As C WITH (NOLOCK)
Left Join Tasks t WITH (NOLOCK)
On t.TargetID = C.ContactId
And t.TaskType <> '[=] Auto Email'
LEFT JOIN Users U WITH (NOLOCK)
ON u.UserID = t.UserID
WHERE iLeadPartnerID IS NOT NULL
AND ( c.CreatedOn BETWEEN @startDate AND @endDate )
AND MarketSource IS NOT NULL
AND c.WorkgroupId = @workgroupID
AND Len(c.MarketSource) > 0
GROUP BY c.MarketSource, u.LastName, u.FirstName, c.ContactId
, c.LastName, c.FirstName, c.MiddleName, c.CreatedOn, t.TaskType
, t.Result, t.Workflow_ResultChosen_Label
ORDER BY c.LastName, c.MiddleName, c.FirstName
精彩评论