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 AX (Archived)

Conditionnal order by clauses in x++ sql select statement?

(0) ShareShare
ReportReport
Posted on by

Is there any way to put conditional statements in an order by?

I'd like to do something like this:

order by prod.ProdPrio desc, (prod.ProjId == "" ? prod.ProdId : prod.SchedStart) asc


*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    You can put the fields in order and it then will be applied on the select in the order you specified the fields

    order by ProdPrio desc, ProdId asc, SchedStart asc

    This would first sort by ProdId and then SchedStart. So for those records where ProdId is the same (which includes empty), it will be sorted by SchedStart. I don't think it will be an issue if the records which do have a ProdId will also be sorted by SchedStart?

  • Community Member Profile Picture
    on at

    In fact, the condition is on ProJid, while I want to sort by ProDid.

    So, if there is a project attached, I want to sort by date, but if there is no project id I want to sort by ProdId.

  • Community Member Profile Picture
    on at

    Oh sorry, I misread that as two times ProdId. I never saw something like this. Maybe you can split it up into two selects?

  • Community Member Profile Picture
    on at

    If I slit it up, I'd need to re-order them afterwards... and my queryr needs them to be ordered because I exit the while when a certain amount of lines are reached.

  • Suggested answer
    Bashir Ahmad Profile Picture
    5,248 on at

    you need two select commands one with sort by date and one with sort by prodID

    place a if condition if (project attached)

    { Query sort by date} ELSE {Query sort by ProdId}

  • Suggested answer
    5400 Profile Picture
    7,162 on at

    In single select statement, you can not put two type of sort with different field.

    you can achieve through querybuilddatasource  and ordermode function with  OR condition.

  • Suggested answer
    jasman Profile Picture
    1,413 on at

    You can not tell the database to alter the sorting order while it is running the query.

    But maybe you could do two queries:

    One with

       where prod.ProjId == ""

       order by prod.ProdPrio desc, prod.ProdId

    And one with

       where prod.ProjId != ""

       order by prod.ProdPrio desc, prod.SchedStart asc

    And then make another query doing a union on the two first queries.

    Then you could apply sorting to the union query.

  • Verified answer
    Community Member Profile Picture
    on at

    What you'd like to do isn't possible in SQL. The ORDER BY is done over a resultset not for each row. That makes prod.ProjId == "" irrelevant.

    What is possible is to put an 'CASE Prod.ProjId = "" THEN Prod.ProdId ELSE Prod.SchedStart END AS fieldName' in the select clause and use 'fieldName ASC' in the ORDER BY clause.

    Doing this in X++ is not possible. but what you could do is create a view with a view method that contains the 'CASE Prod.ProjId = "" THEN Prod.ProdId ELSE Prod.SchedStart END'. Use this viewmethod in a new field. You can query the view in X++ as if it is a table now.

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 AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans