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

  • Community Member Profile Picture
    on at
    RE: QueryExpression With Multiple Join Conditions

    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

  • ashlega Profile Picture
    34,477 on at
    RE: QueryExpression With Multiple Join Conditions

    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

  • Verified answer
    Srujan K Profile Picture
    317 on at
    RE: QueryExpression With Multiple Join Conditions

    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.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans