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

Announcements

No record found.

News and Announcements icon
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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans