web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

QueryExpression With Multiple Join Conditions

(1) ShareShare
ReportReport
Posted on by

Hi,

I am trying to translate the following SQL query to a QueryExpression:

SELECT  a.AccountId, o.opportunityid
FROM    dbo.Account a
        INNER JOIN dbo.Opportunity o ON o.CustomerId = a.AccountId
        LEFT JOIN dbo.new_creditpackagefile cpf ON cpf.new_accountid = o.CustomerId
                                                             AND cpf.new_opportunityid = o.OpportunityId
                                                             AND cpf.new_documenttype = 32;


Here's how I would rewrite the above SQL to be possible via QueryExpression:

SELECT  DISTINCT a.AccountId, o.opportunityid
FROM    dbo.Account a
        INNER JOIN dbo.Opportunity o ON o.CustomerId = a.AccountId
        LEFT JOIN dbo.new_creditpackagefile cpf ON cpf.new_accountid = o.CustomerId AND cpf.new_documenttype = 32
	LEFT JOIN dbo.new_creditpackagefile cpf2 ON cpf2.new_opportunityid = o.OpportunityId AND cpf2.new_documenttype = 32


The downside of the second query is that it uses another left join and the DISTINCT operator. 

Questions:

  1. Do you think the additional join and DISTINCT operator would have a meaningful performance hit?
  2. Will there be support for multiple join conditions in the future?

Thank you very much!

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Srujan K Profile Picture
    317 on at

    Yes there would be definitely a performance hit when you use distinct , best thing is to get the RAW data from CRM and play around with LINQ queries.

  • ashlega Profile Picture
    34,477 on at

    As an option, you might create a text field on the opportunity entity, store a combination of those guids there (accountid_opportunitid), do the same for the new_creditpackagefile  entity, and, then, use a join on those new fields

  • Community Member Profile Picture
    on at

    Thank you for taking the time to answer my question. The LINQ route might be easiest without having to create any additional attributes.

    By the way: here's another answer that I really liked from CRMUG forums: https://goo.gl/481Wqt

    I did not even think to simplify the SQL query.

    Thank you,

    Tony

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans