title: Invalid operation: syntax error at or near "Delta"
body: <p>I have redshift script</p>
  
  <p>Here is the code of  part of a script</p>
  
  <pre><code>    SELECT  cog.organizationid,
          cog.clientid,
          t.MonthBilled,
          t.Month,
          t.Year,
          u.ClientCharges,
          cp.supervisorcontactid AS Principal,
          cc.fullname AS CSL,
  
          LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) AS PreviosMonthCharges,
  
          CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END -
                                  CASE WHEN LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
                                  ELSE LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month)
                                  END AS Delta,
  
          CASE WHEN (CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END - CASE WHEN LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
                                  ELSE LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) END) &gt; 0 THEN 'Gain'
               WHEN CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END - CASE WHEN LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
                                  ELSE LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) END &lt; 0 THEN 'Loss'
                                  ELSE NULL END AS GainsLosses,
          CASE WHEN u.ClientCharges &gt;=0 AND LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL AND t.MonthBilled &lt;&gt; '1/1/2015' THEN 'New' ELSE 'Existing' END AS ClientType,
  
          CASE WHEN u.ClientCharges IS NULL THEN 'InActive' ELSE 'Active' END AS ActiveStatus,
  
          CASE WHEN LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NOT NULL AND t.MonthBilled &lt;&gt; '1/1/2015' THEN 1 ELSE NULL END AS ActiveLastMonth,
  
          CASE WHEN u.ClientCharges IS NULL AND (LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) &lt;&gt; 0 AND LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NOT NULL) AND t.MonthBilled &lt;&gt; '1/1/2015' THEN 1 ELSE NULL END AS OneMonthChurn,
  
          u2.FeatureKeyCount,
  
          CASE WHEN u.ClientCharges IS NOT NULL AND u.ClientCharges &gt; 0 THEN 1 ELSE 0 END AS OrgCounter
  FROM    public.contacts_client_organization cog
  </code></pre>
  
  <p>I have this error message when trying to run it</p>
  
  <blockquote>
    <p>[42601][500310] Amazon Invalid operation: syntax error at or near "Delta" Position: 747; java.lang.RuntimeException: com.amazon.support.exceptions.ErrorException: Amazon Invalid operation: syntax error at or near "Delta" Position: 747;</p>
  </blockquote>
  
  <p>But I not understood, what's wrong?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here is rewritten part with valid <em>LAG</em>, but I still have the same problem</p>
  
  <pre><code> SELECT  cog.organizationid,
          cog.clientid,
          t.MonthBilled,
          t.Month,
          t.Year,
          u.ClientCharges,
          cp.supervisorcontactid AS Principal,
          cc.fullname AS CSL,
  
          LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) AS PreviosMonthCharges,
  
          CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END -
                                  CASE WHEN LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
                                  ELSE LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month)
                                  END AS Delta,
  
          CASE WHEN (CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END - CASE WHEN LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
                                  ELSE LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) END) &gt; 0 THEN 'Gain'
               WHEN CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END - CASE WHEN LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
                                  ELSE LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) END &lt; 0 THEN 'Loss'
                                  ELSE NULL END AS GainsLosses,
          CASE WHEN u.ClientCharges &gt;=0 AND LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL AND t.MonthBilled &lt;&gt; '1/1/2015' THEN 'New' ELSE 'Existing' END AS ClientType,
  
          CASE WHEN u.ClientCharges IS NULL THEN 'InActive' ELSE 'Active' END AS ActiveStatus,
  
          CASE WHEN LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NOT NULL AND t.MonthBilled &lt;&gt; '1/1/2015' THEN 1 ELSE NULL END AS ActiveLastMonth,
  
          CASE WHEN u.ClientCharges IS NULL AND (LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) &lt;&gt; 0 AND LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NOT NULL) AND t.MonthBilled &lt;&gt; '1/1/2015' THEN 1 ELSE NULL END AS OneMonthChurn,
  
          u2.FeatureKeyCount,
  
          CASE WHEN u.ClientCharges IS NOT NULL AND u.ClientCharges &gt; 0 THEN 1 ELSE 0 END AS OrgCounter
  FROM    public.contacts_client_organization cog
  </code></pre>