Skip to content

Instantly share code, notes, and snippets.

@TomDeMille
Created August 31, 2018 16:56
Show Gist options
  • Select an option

  • Save TomDeMille/455a670c3d3ce657d4eb380742e5a2e2 to your computer and use it in GitHub Desktop.

Select an option

Save TomDeMille/455a670c3d3ce657d4eb380742e5a2e2 to your computer and use it in GitHub Desktop.
BEST fix (if we can limit by typeid)
SELECT C.ContactId ,
C.TenantID ,
C.WebVisitorID ,
C.FirstName ,
C.LastName ,
( SELECT TOP 1
Name
FROM LeadSource
WHERE Alias = WebDetail.OriginalSource
) AS SourceName ,
Email.EmailAddress ,
C.BestPhone ,
( SELECT TOP 1
PhotoLink
FROM ContactSocialDetails
WHERE ContactID = C.ContactId
AND PhotoLink IS NOT NULL
ORDER BY SocialType
) AS PhotoLink ,
C.ContactOriginTypeID ,
C.DateCreated ,
C.DateModified
FROM Contact C
inner join ContactUser CU1 on CU1.ContactID = c.ContactID and CU1.UserID = 579 and CU1.ContactUserTypeID = 1
OUTER APPLY ( SELECT TOP 1
CE.EmailAddress
FROM ContactEmail CE
WHERE CE.ContactId = C.ContactID
AND CE.IsPrimary = 1
) AS Email
OUTER APPLY ( SELECT TOP 1
CWD.OriginalSource
FROM ContactWebDetail CWD
WHERE CWD.ContactId = C.ContactID
) AS WebDetail
WHERE C.TenantID = 489
ORDER BY cu1.AssignedDate DESC
OFFSET 100 ROWS FETCH NEXT 250 ROWS ONLY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment