<?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>question Get an end date, (n) workdays from a given date, using a calendar table in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/376990#M243092</link>
    <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="s300570_0-1695908985505.png" style="width: 524px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/38542i738CEDF887D7717B/image-dimensions/524x253?v=v2" width="524" height="253" role="button" title="s300570_0-1695908985505.png" alt="s300570_0-1695908985505.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Impala version :&amp;nbsp;&lt;SPAN&gt;impalad&amp;nbsp;version&amp;nbsp;2.12.0-cdh5.16.2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;As shown above, I have two tables; Customer table and Calendar Table, wich columns I describe below:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;&lt;U&gt;Customer Table:&lt;/U&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;[Customer] : Customer ID&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;[Date] : Creation date&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;[Num_Days] : Number of working days to calculate the [next_wkday] in the query result.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;FONT size="4"&gt;Calendar Table:&lt;/FONT&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;[civil_util] : when (1) -&amp;gt; workday; when (0) -&amp;gt; non working day&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;I need to get the query_result with the three columns of Customer Table and a calculated date [next_wkday], representing the number of working days [Num_Days] after the&amp;nbsp;Creation date, jumping the zeros in the [civil_util] column.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;The query below calculates the [next_wkday] using the Lead() function. But is not a solution because the offset parameter must be a constant, and we need to use the [Num_Days] value for each Creation Date:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;select *,
            Lead (to_date(ref_date),5) OVER (ORDER BY to_date(ref_date)) AS next_wkday,
            datediff(Lead (to_date(ref_date),5) OVER (ORDER BY 
 to_date(ref_date)),ref_date) as days_diff
        from cd_estruturais.calendario_datas
        where
            ref_date &amp;gt;= to_date(now())
            and civil_util = 1
            limit 1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, we need to find another solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to say, for performance puposes, that the Customer table has 4 billion records approximatly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help please?&lt;/P&gt;</description>
    <pubDate>Thu, 28 Sep 2023 14:23:54 GMT</pubDate>
    <dc:creator>s300570</dc:creator>
    <dc:date>2023-09-28T14:23:54Z</dc:date>
    <item>
      <title>Get an end date, (n) workdays from a given date, using a calendar table</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/376990#M243092</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="s300570_0-1695908985505.png" style="width: 524px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/38542i738CEDF887D7717B/image-dimensions/524x253?v=v2" width="524" height="253" role="button" title="s300570_0-1695908985505.png" alt="s300570_0-1695908985505.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Impala version :&amp;nbsp;&lt;SPAN&gt;impalad&amp;nbsp;version&amp;nbsp;2.12.0-cdh5.16.2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;As shown above, I have two tables; Customer table and Calendar Table, wich columns I describe below:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;&lt;U&gt;Customer Table:&lt;/U&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;[Customer] : Customer ID&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;[Date] : Creation date&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;[Num_Days] : Number of working days to calculate the [next_wkday] in the query result.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;FONT size="4"&gt;Calendar Table:&lt;/FONT&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;[civil_util] : when (1) -&amp;gt; workday; when (0) -&amp;gt; non working day&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;I need to get the query_result with the three columns of Customer Table and a calculated date [next_wkday], representing the number of working days [Num_Days] after the&amp;nbsp;Creation date, jumping the zeros in the [civil_util] column.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="4"&gt;The query below calculates the [next_wkday] using the Lead() function. But is not a solution because the offset parameter must be a constant, and we need to use the [Num_Days] value for each Creation Date:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;select *,
            Lead (to_date(ref_date),5) OVER (ORDER BY to_date(ref_date)) AS next_wkday,
            datediff(Lead (to_date(ref_date),5) OVER (ORDER BY 
 to_date(ref_date)),ref_date) as days_diff
        from cd_estruturais.calendario_datas
        where
            ref_date &amp;gt;= to_date(now())
            and civil_util = 1
            limit 1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, we need to find another solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to say, for performance puposes, that the Customer table has 4 billion records approximatly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help please?&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 14:23:54 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/376990#M243092</guid>
      <dc:creator>s300570</dc:creator>
      <dc:date>2023-09-28T14:23:54Z</dc:date>
    </item>
    <item>
      <title>Re: Get an end date, (n) workdays from a given date, using a calendar table</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/376991#M243093</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/107162"&gt;@s300570&lt;/a&gt;&amp;nbsp;Welcome to the Cloudera Community!&lt;BR /&gt;&lt;BR /&gt;To help you get the best possible solution, I have tagged our experts&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/9634"&gt;@bbreak&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/38161"&gt;@cravani&lt;/a&gt;&amp;nbsp; who may be able to assist you further.&lt;BR /&gt;&lt;BR /&gt;Please keep us updated on your post, and we hope you find a satisfactory solution to your query.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Sep 2023 16:41:28 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/376991#M243093</guid>
      <dc:creator>DianaTorres</dc:creator>
      <dc:date>2023-09-28T16:41:28Z</dc:date>
    </item>
    <item>
      <title>Re: Get an end date, (n) workdays from a given date, using a calendar table</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/377038#M243104</link>
      <description>&lt;P&gt;My best regards to you, Diana.&lt;BR /&gt;I'll just keep waiting for help from those experts, or there must be any action from me?&lt;BR /&gt;&lt;BR /&gt;Leopoldo Fernandes&lt;BR /&gt;Portugal&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2023 10:46:24 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/377038#M243104</guid>
      <dc:creator>s300570</dc:creator>
      <dc:date>2023-09-29T10:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: Get an end date, (n) workdays from a given date, using a calendar table</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/377062#M243116</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/34263"&gt;@JoseManuel&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/70785"&gt;@Shmoo&lt;/a&gt;&amp;nbsp;Any insights here?&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2023 18:43:03 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/377062#M243116</guid>
      <dc:creator>DianaTorres</dc:creator>
      <dc:date>2023-09-29T18:43:03Z</dc:date>
    </item>
    <item>
      <title>Re: Get an end date, (n) workdays from a given date, using a calendar table</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/377357#M243238</link>
      <description>&lt;P&gt;Hi, Can anyone help please?&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 11:05:16 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/377357#M243238</guid>
      <dc:creator>s300570</dc:creator>
      <dc:date>2023-10-09T11:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: Get an end date, (n) workdays from a given date, using a calendar table</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/377361#M243240</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/107162"&gt;@s300570&lt;/a&gt;,&lt;BR /&gt;&lt;BR /&gt;If I understood your requirement correctly, you might try something like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;WITH WorkingDaysCTE AS (
  SELECT
    ref_date,
    ROW_NUMBER() OVER (ORDER BY ref_date) AS row_num
  FROM
    cd_estruturais.calendario_datas
  WHERE
    ref_date &amp;gt;= CURRENT_DATE -- You can adjust the starting date as needed
    AND civil_util = 1
)

SELECT
  c1.*,
  w1.ref_date AS next_wkday,
  DATEDIFF(w1.ref_date, c1.ref_date) AS Num_Days
FROM
  cd_estruturais.calendario_datas c1
JOIN
  WorkingDaysCTE w1 ON c1.ref_date &amp;lt;= w1.ref_date
                    AND w1.row_num = (
                      SELECT MIN(row_num)
                      FROM WorkingDaysCTE w2
                      WHERE w2.row_num &amp;gt; w1.row_num
                    )
WHERE
  c1.ref_date &amp;gt;= CURRENT_DATE
  AND c1.civil_util = 1
LIMIT 1;&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;Now, just as info, as I no longer have access to an Impala/Hive system, the above query was written as a standard SQL, so you might want to double check the syntax.&lt;BR /&gt;&lt;BR /&gt;In terms of explanations:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;The common table expression (CTE) named WorkingDaysCTE is used to filter the dates with civil_util = 1 and assign a row number to each date based on their order.&lt;/LI&gt;&lt;LI&gt;The main query then joins the calendario_datas table with the CTE on the condition that the date in the calendar table is less than or equal to the date in the CTE.&lt;/LI&gt;&lt;LI&gt;The subquery in the SELECT clause is used to find the minimum row number greater than the current row number in the CTE. This helps in getting the next working day.&lt;/LI&gt;&lt;LI&gt;The DATEDIFF function is used to calculate the number of days between the ref_date and the next_wkday.&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Mon, 09 Oct 2023 13:22:58 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/377361#M243240</guid>
      <dc:creator>cotopaul</dc:creator>
      <dc:date>2023-10-09T13:22:58Z</dc:date>
    </item>
    <item>
      <title>Re: Get an end date, (n) workdays from a given date, using a calendar table</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/377592#M243302</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/107162"&gt;@s300570&lt;/a&gt;&amp;nbsp;Has the reply helped resolve your issue? If so, please mark the appropriate reply as the solution, as it will make it easier for others to find the answer in the future. Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2023 18:02:37 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Get-an-end-date-n-workdays-from-a-given-date-using-a/m-p/377592#M243302</guid>
      <dc:creator>DianaTorres</dc:creator>
      <dc:date>2023-10-12T18:02:37Z</dc:date>
    </item>
  </channel>
</rss>

