Answers.Body: <p>Digging around a bit, it looks like more complex order queries need to be fully elaborated. I adjusted as follows for it to work:</p>
  <pre><code>orderBy : &quot;DATEDIFF(`content`.`field_screeningDateFirst`, `content`.`field_screeningDateLast`) DESC&quot;,
  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-27T01:43:28.680Z
Answers.LastActivityDate: 2020-10-27T01:53:21.363Z
Answers.LastEditDate: 2020-10-27T01:53:21.363Z
Answers.UserDisplayName: Document Services
Body: <p>I'm trying to order the results of an entries query by the difference between two date fields but I can't seem to get it to work.</p>
  <p>Here is my query:</p>
  <pre><code>{% set todaysStreamsParams = {
    section : 'films',
    orderBy : &quot;DATEDIFF(`screeningDateFirst`, `screeningDateLast`) DESC&quot;,
    limit   : 5,
  } %}
  
  {% set todaysStreams = craft.entries(todaysStreamsParams).all() %}
  </code></pre>
  <p>But I'm getting the following database exception:</p>
  <pre><code>Unknown column 'screeningDateFirst' in 'order clause.
  </code></pre>
  <p>Any tips?</p>
  
Tags: <entries><query><ordering>
Title: Order entries by DATEDIFF
ThreadText: # Order entries by DATEDIFF
  
  (Asked by: username_0 on 2020-10-27)
  
  I'm trying to order the results of an entries query by the difference between two date fields but I can't seem to get it to work.
  
  Here is my query:
  
  ```
  {% set todaysStreamsParams = {
    section : 'films',
    orderBy : "DATEDIFF(`screeningDateFirst`, `screeningDateLast`) DESC",
    limit   : 5,
  } %}
  
  {% set todaysStreams = craft.entries(todaysStreamsParams).all() %}
  
  ```
  
  But I'm getting the following database exception:
  
  ```
  Unknown column 'screeningDateFirst' in 'order clause.
  
  ```
  
  Any tips?
  
  **Tags:** entries, query, ordering
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-10-27. Score: 1 | Accepted answer
  
  Digging around a bit, it looks like more complex order queries need to be fully elaborated. I adjusted as follows for it to work:
  
  ```
  orderBy : "DATEDIFF(`content`.`field_screeningDateFirst`, `content`.`field_screeningDateLast`) DESC",
  
  ```
  
  
  ---
  
  