<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Can we use analytic functions like LEAD or LAG in Incorta Sparksql? in Data &amp; Schema Discussions</title>
    <link>https://community.incorta.com/t5/data-schema-discussions/can-we-use-analytic-functions-like-lead-or-lag-in-incorta/m-p/2647#M166</link>
    <description>&lt;P&gt;Yes, we can use window functions in Incorta spark SQL using 'PARTITION BY',&amp;nbsp;refer to the sample SQL&amp;nbsp;below -&lt;/P&gt;
&lt;P&gt;Also refer to this useful link&amp;nbsp; -&amp;nbsp;&lt;A href="https://databricks.com/blog/2015/07/15/introducing-window-functions-in-spark-sql.html" target="_blank" rel="nofollow noopener noreferrer"&gt;https://databricks.com/blog/2015/07/15/introducing-window-functions-in-spark-sql.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;1) Example 1 using&amp;nbsp;LAG(),&amp;nbsp;compares current and previous month numbers.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;SELECT X.MONTH_SEQ,X.MONTH_YEAR,X.ITEM_NUMBER,
CASE WHEN X.SALES_AMT &amp;gt; 100 AND X.PREVMONTHAMT &amp;gt; 100 THEN 'EXISTING'
     WHEN X.SALES_AMT &amp;gt; 100 AND X.PREVMONTHAMT &amp;lt; 100 THEN 'NEW'
     WHEN X.SALES_AMT &amp;lt; 100 AND X.PREVMONTHAMT &amp;lt; 100 THEN 'INACTIVE'
END PROD_STATUS,
X.SALES_AMT,
X.PREVMONTHAMT
FROM
(SELECT S.MONTH_SEQ,S.MONTH_YEAR,S.ITEM_NUMBER,S.SALES_AMT,
LAG(S.SALES_AMT,1,0 ) OVER ( partition by S.ITEM_NUMBER ORDER BY S.ITEM_NUMBER,S.MONTH_SEQ) AS PREVMONTHAMT
FROM test.SAMPLE_SALES S
)X&lt;/LI-CODE&gt;
&lt;P&gt;&lt;SPAN&gt;2) Example 2 for computing the end date using LEAD()&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;SELECT X.PERSON_ID,
X.EFFECTIVE_START_DATE,
coalesce(date_sub(X.EFFECTIVE_END_DATE,1),TO_DATE('2099-01-01','yyyy-MM-dd')) EFFECTIVE_END_DATE,
(case when X.EFFECTIVE_END_DATE  IS NULL then 'Y' else 'N' end) current_flag
FROM
  (SELECT
PERSON_ID, EFFECTIVE_START_DATE,
LEAD(EFFECTIVE_START_DATE,1) OVER ( partition by PERSON_ID ORDER BY PERSON_ID, EFFECTIVE_START_DATE) AS EFFECTIVE_END_DATE
FROM
EBS_HR_COMMON.PER_ALL_PEOPLE_F
WHERE 1=1) X&lt;/LI-CODE&gt;</description>
    <pubDate>Mon, 19 Sep 2022 20:00:41 GMT</pubDate>
    <dc:creator>amit_kothari</dc:creator>
    <dc:date>2022-09-19T20:00:41Z</dc:date>
    <item>
      <title>Can we use analytic functions like LEAD or LAG in Incorta Sparksql?</title>
      <link>https://community.incorta.com/t5/data-schema-discussions/can-we-use-analytic-functions-like-lead-or-lag-in-incorta/m-p/2646#M165</link>
      <description>&lt;P class="topic__title"&gt;Can we use analytic functions like LEAD or LAG in Incorta Sparksql?&lt;/P&gt;
&lt;DIV class="topic-meta-wrap"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Mon, 19 Sep 2022 19:59:26 GMT</pubDate>
      <guid>https://community.incorta.com/t5/data-schema-discussions/can-we-use-analytic-functions-like-lead-or-lag-in-incorta/m-p/2646#M165</guid>
      <dc:creator>amit_kothari</dc:creator>
      <dc:date>2022-09-19T19:59:26Z</dc:date>
    </item>
    <item>
      <title>Re: Can we use analytic functions like LEAD or LAG in Incorta Sparksql?</title>
      <link>https://community.incorta.com/t5/data-schema-discussions/can-we-use-analytic-functions-like-lead-or-lag-in-incorta/m-p/2647#M166</link>
      <description>&lt;P&gt;Yes, we can use window functions in Incorta spark SQL using 'PARTITION BY',&amp;nbsp;refer to the sample SQL&amp;nbsp;below -&lt;/P&gt;
&lt;P&gt;Also refer to this useful link&amp;nbsp; -&amp;nbsp;&lt;A href="https://databricks.com/blog/2015/07/15/introducing-window-functions-in-spark-sql.html" target="_blank" rel="nofollow noopener noreferrer"&gt;https://databricks.com/blog/2015/07/15/introducing-window-functions-in-spark-sql.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;1) Example 1 using&amp;nbsp;LAG(),&amp;nbsp;compares current and previous month numbers.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;SELECT X.MONTH_SEQ,X.MONTH_YEAR,X.ITEM_NUMBER,
CASE WHEN X.SALES_AMT &amp;gt; 100 AND X.PREVMONTHAMT &amp;gt; 100 THEN 'EXISTING'
     WHEN X.SALES_AMT &amp;gt; 100 AND X.PREVMONTHAMT &amp;lt; 100 THEN 'NEW'
     WHEN X.SALES_AMT &amp;lt; 100 AND X.PREVMONTHAMT &amp;lt; 100 THEN 'INACTIVE'
END PROD_STATUS,
X.SALES_AMT,
X.PREVMONTHAMT
FROM
(SELECT S.MONTH_SEQ,S.MONTH_YEAR,S.ITEM_NUMBER,S.SALES_AMT,
LAG(S.SALES_AMT,1,0 ) OVER ( partition by S.ITEM_NUMBER ORDER BY S.ITEM_NUMBER,S.MONTH_SEQ) AS PREVMONTHAMT
FROM test.SAMPLE_SALES S
)X&lt;/LI-CODE&gt;
&lt;P&gt;&lt;SPAN&gt;2) Example 2 for computing the end date using LEAD()&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;SELECT X.PERSON_ID,
X.EFFECTIVE_START_DATE,
coalesce(date_sub(X.EFFECTIVE_END_DATE,1),TO_DATE('2099-01-01','yyyy-MM-dd')) EFFECTIVE_END_DATE,
(case when X.EFFECTIVE_END_DATE  IS NULL then 'Y' else 'N' end) current_flag
FROM
  (SELECT
PERSON_ID, EFFECTIVE_START_DATE,
LEAD(EFFECTIVE_START_DATE,1) OVER ( partition by PERSON_ID ORDER BY PERSON_ID, EFFECTIVE_START_DATE) AS EFFECTIVE_END_DATE
FROM
EBS_HR_COMMON.PER_ALL_PEOPLE_F
WHERE 1=1) X&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 19 Sep 2022 20:00:41 GMT</pubDate>
      <guid>https://community.incorta.com/t5/data-schema-discussions/can-we-use-analytic-functions-like-lead-or-lag-in-incorta/m-p/2647#M166</guid>
      <dc:creator>amit_kothari</dc:creator>
      <dc:date>2022-09-19T20:00:41Z</dc:date>
    </item>
  </channel>
</rss>

