<?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: Recrusive cte in databrick sql in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/48018#M28222</link>
    <description>&lt;PRE&gt;&lt;SPAN class=""&gt;need to run below query in databricks ql but it showing error as UserCTE table not found.&lt;BR /&gt;&lt;BR /&gt;WITH&lt;/SPAN&gt; UserCTE &lt;SPAN class=""&gt;AS&lt;/SPAN&gt; (
  &lt;SPAN class=""&gt;SELECT&lt;/SPAN&gt; userId, userName, managerId, &lt;SPAN class=""&gt;0&lt;/SPAN&gt; &lt;SPAN class=""&gt;AS&lt;/SPAN&gt; EmpLevel
  &lt;SPAN class=""&gt;FROM&lt;/SPAN&gt; Users &lt;SPAN class=""&gt;where&lt;/SPAN&gt; managerId &lt;SPAN class=""&gt;IS&lt;/SPAN&gt; &lt;SPAN class=""&gt;NULL&lt;/SPAN&gt;

  &lt;SPAN class=""&gt;UNION&lt;/SPAN&gt; &lt;SPAN class=""&gt;ALL&lt;/SPAN&gt;

  &lt;SPAN class=""&gt;SELECT&lt;/SPAN&gt; usr.userId, usr.userName, usr.managerId, mgr.[EmpLevel]&lt;SPAN class=""&gt;+&lt;/SPAN&gt;&lt;SPAN class=""&gt;1&lt;/SPAN&gt;
  &lt;SPAN class=""&gt;FROM&lt;/SPAN&gt; Users &lt;SPAN class=""&gt;AS&lt;/SPAN&gt; usr
    &lt;SPAN class=""&gt;INNER&lt;/SPAN&gt; &lt;SPAN class=""&gt;JOIN&lt;/SPAN&gt; UserCTE &lt;SPAN class=""&gt;AS&lt;/SPAN&gt; mgr
      &lt;SPAN class=""&gt;ON&lt;/SPAN&gt; usr.managerId &lt;SPAN class=""&gt;=&lt;/SPAN&gt; mgr.userId &lt;SPAN class=""&gt;where&lt;/SPAN&gt; usr.managerId &lt;SPAN class=""&gt;IS&lt;/SPAN&gt; &lt;SPAN class=""&gt;NOT&lt;/SPAN&gt; &lt;SPAN class=""&gt;NULL&lt;/SPAN&gt;
)
&lt;SPAN class=""&gt;SELECT&lt;/SPAN&gt; &lt;SPAN class=""&gt;*&lt;/SPAN&gt; 
  &lt;SPAN class=""&gt;FROM&lt;/SPAN&gt; UserCTE &lt;SPAN class=""&gt;AS&lt;/SPAN&gt; u &lt;SPAN class=""&gt;where&lt;/SPAN&gt; u.ManagerId&lt;SPAN class=""&gt;=&lt;/SPAN&gt;&lt;SPAN class=""&gt;3&lt;/SPAN&gt;
  &lt;SPAN class=""&gt;ORDER&lt;/SPAN&gt; &lt;SPAN class=""&gt;BY&lt;/SPAN&gt; EmpLevel&lt;BR /&gt;&lt;BR /&gt;Please help me on this&lt;/PRE&gt;</description>
    <pubDate>Wed, 04 Oct 2023 06:24:55 GMT</pubDate>
    <dc:creator>Databricks143</dc:creator>
    <dc:date>2023-10-04T06:24:55Z</dc:date>
    <item>
      <title>Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/47999#M28219</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;How to write recrusive cte in databricks SQL.&lt;/P&gt;&lt;P&gt;Please let me know any one have solution for this&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2023 04:54:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/47999#M28219</guid>
      <dc:creator>Databricks143</dc:creator>
      <dc:date>2023-10-04T04:54:43Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/48018#M28222</link>
      <description>&lt;PRE&gt;&lt;SPAN class=""&gt;need to run below query in databricks ql but it showing error as UserCTE table not found.&lt;BR /&gt;&lt;BR /&gt;WITH&lt;/SPAN&gt; UserCTE &lt;SPAN class=""&gt;AS&lt;/SPAN&gt; (
  &lt;SPAN class=""&gt;SELECT&lt;/SPAN&gt; userId, userName, managerId, &lt;SPAN class=""&gt;0&lt;/SPAN&gt; &lt;SPAN class=""&gt;AS&lt;/SPAN&gt; EmpLevel
  &lt;SPAN class=""&gt;FROM&lt;/SPAN&gt; Users &lt;SPAN class=""&gt;where&lt;/SPAN&gt; managerId &lt;SPAN class=""&gt;IS&lt;/SPAN&gt; &lt;SPAN class=""&gt;NULL&lt;/SPAN&gt;

  &lt;SPAN class=""&gt;UNION&lt;/SPAN&gt; &lt;SPAN class=""&gt;ALL&lt;/SPAN&gt;

  &lt;SPAN class=""&gt;SELECT&lt;/SPAN&gt; usr.userId, usr.userName, usr.managerId, mgr.[EmpLevel]&lt;SPAN class=""&gt;+&lt;/SPAN&gt;&lt;SPAN class=""&gt;1&lt;/SPAN&gt;
  &lt;SPAN class=""&gt;FROM&lt;/SPAN&gt; Users &lt;SPAN class=""&gt;AS&lt;/SPAN&gt; usr
    &lt;SPAN class=""&gt;INNER&lt;/SPAN&gt; &lt;SPAN class=""&gt;JOIN&lt;/SPAN&gt; UserCTE &lt;SPAN class=""&gt;AS&lt;/SPAN&gt; mgr
      &lt;SPAN class=""&gt;ON&lt;/SPAN&gt; usr.managerId &lt;SPAN class=""&gt;=&lt;/SPAN&gt; mgr.userId &lt;SPAN class=""&gt;where&lt;/SPAN&gt; usr.managerId &lt;SPAN class=""&gt;IS&lt;/SPAN&gt; &lt;SPAN class=""&gt;NOT&lt;/SPAN&gt; &lt;SPAN class=""&gt;NULL&lt;/SPAN&gt;
)
&lt;SPAN class=""&gt;SELECT&lt;/SPAN&gt; &lt;SPAN class=""&gt;*&lt;/SPAN&gt; 
  &lt;SPAN class=""&gt;FROM&lt;/SPAN&gt; UserCTE &lt;SPAN class=""&gt;AS&lt;/SPAN&gt; u &lt;SPAN class=""&gt;where&lt;/SPAN&gt; u.ManagerId&lt;SPAN class=""&gt;=&lt;/SPAN&gt;&lt;SPAN class=""&gt;3&lt;/SPAN&gt;
  &lt;SPAN class=""&gt;ORDER&lt;/SPAN&gt; &lt;SPAN class=""&gt;BY&lt;/SPAN&gt; EmpLevel&lt;BR /&gt;&lt;BR /&gt;Please help me on this&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Oct 2023 06:24:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/48018#M28222</guid>
      <dc:creator>Databricks143</dc:creator>
      <dc:date>2023-10-04T06:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/48159#M28258</link>
      <description>&lt;P&gt;recursive CTE is not supported in spark. but there is a workaround using python:&lt;/P&gt;&lt;P&gt;&lt;A href="https://medium.com/@24chynoweth/recursive-cte-on-databricks-2ac0dff8ca06" target="_self"&gt;https://medium.com/@24chynoweth/recursive-cte-on-databricks-2ac0dff8ca06&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2023 14:47:54 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/48159#M28258</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2023-10-04T14:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/48558#M28331</link>
      <description>&lt;P&gt;Thanks for update .&lt;/P&gt;&lt;P&gt;We have frame work&amp;nbsp; restrictions in databricks&amp;nbsp; in my organisation and&amp;nbsp; we need to use only scala code&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 07:18:13 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/48558#M28331</guid>
      <dc:creator>Databricks143</dc:creator>
      <dc:date>2023-10-06T07:18:13Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/48567#M28336</link>
      <description>&lt;P&gt;the code is easily translated to scala.&lt;BR /&gt;There is nothing python specific about it.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 07:58:32 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/48567#M28336</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2023-10-06T07:58:32Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/68286#M33626</link>
      <description>&lt;P&gt;Did you find a solution? I have an almost identical CTE query to pull employee hierarchy info and I am receiving the same error&lt;/P&gt;</description>
      <pubDate>Mon, 06 May 2024 14:50:27 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/68286#M33626</guid>
      <dc:creator>A_Kennedy</dc:creator>
      <dc:date>2024-05-06T14:50:27Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/68289#M33628</link>
      <description>&lt;P&gt;It is still not supported.&amp;nbsp; Not sure when it will be (if ever).&lt;/P&gt;</description>
      <pubDate>Mon, 06 May 2024 14:53:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/68289#M33628</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2024-05-06T14:53:11Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/68445#M33680</link>
      <description>&lt;P&gt;Weirdly, if you ask the Databricks assistant, it tells you it does support recursive CTE and will give you sample code for it.&amp;nbsp;If you follow up and press it on details it doubles down and says it supports it, but tell you the runtime version information.&lt;/P&gt;&lt;P&gt;I'm wondering if support is in development.&lt;/P&gt;</description>
      <pubDate>Tue, 07 May 2024 12:29:41 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/68445#M33680</guid>
      <dc:creator>EWhitley</dc:creator>
      <dc:date>2024-05-07T12:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/84235#M37169</link>
      <description>&lt;P&gt;Yes , Databricks not supporting recursive.. Same AI Assistant message.. This feature should be enabled here..&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Aug 2024 18:10:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/84235#M37169</guid>
      <dc:creator>Msquare</dc:creator>
      <dc:date>2024-08-26T18:10:03Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/84593#M37209</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/89918"&gt;@Databricks143&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;This code works in Scala. One thing. It has a number of iterations hard-coded to 10. If there are more levels, you need to adjust&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import org.apache.spark.sql.functions._

val data = Seq(
  (1, "Alice", None: Option[Int]),  // Root employee
  (2, "Bob", Some(1)),              // Reports to Alice
  (3, "Charlie", Some(2)),          // Reports to Bob
  (4, "David", Some(1)),            // Reports to Alice
  (5, "Eve", Some(3))               // Reports to Charlie
)

val df = data.toDF("userId", "userName", "managerId")

// Initialize root employee, set EmpLevel to 0
var df_levels = df.filter($"managerId".isNull).withColumn("EmpLevel", lit(0))

// Create a DataFrame to hold the results as they are built up
var final_df = df_levels

// Iteratively process subordinates
val max_iterations = 10  // Adjust based on expected maximum depth
var hasNewLevels = true

for (_ &amp;lt;- 1 to max_iterations if hasNewLevels) {
  // Identify subordinates of employees whose levels have already been calculated
  val df_new_levels = df.as("emp")
    .join(df_levels.as("mgr"), $"emp.managerId" === $"mgr.userId", "inner")
    .select(
      $"emp.userId",
      $"emp.userName",
      $"emp.managerId",
      ($"mgr.EmpLevel" + 1).as("EmpLevel")
    )

  // If no new levels are calculated, set the flag to false to break the loop
  if (df_new_levels.isEmpty) {
    hasNewLevels = false
  } else {
    // Append the new levels to the final result set
    final_df = final_df.union(df_new_levels)
    
    // Update df_levels to include only the newly calculated levels for the next iteration
    df_levels = df_new_levels
  }
}

// Final result
final_df.orderBy("EmpLevel", "userId").show()&lt;/LI-CODE&gt;&lt;P&gt;The output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="filipniziol_0-1724782428698.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/10651iD003A21DB1F2B3FD/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="filipniziol_0-1724782428698.png" alt="filipniziol_0-1724782428698.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2024 18:14:22 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/84593#M37209</guid>
      <dc:creator>filipniziol</dc:creator>
      <dc:date>2024-08-27T18:14:22Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/85722#M37251</link>
      <description>&lt;P&gt;I want to create a persistent view on a recursive cte. Is something like that just not possible?&lt;/P&gt;&lt;P&gt;As far as i understand:&lt;/P&gt;&lt;P&gt;You need to use pyspark to be able to do a recursive cte.&lt;/P&gt;&lt;P&gt;BUT&lt;/P&gt;&lt;P&gt;You&amp;nbsp; need to use Spark SQL to create a persistent view.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any workaround for this? The reason i want to do this is that my data gets updated frequently. My users want to look at the data in a way that requires a recursive cte but they will not access the majority of it frequently, meaning it would be a waste of resources to persist the information in a physical table.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2024 09:06:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/85722#M37251</guid>
      <dc:creator>dlehmann</dc:creator>
      <dc:date>2024-08-28T09:06:35Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/85847#M37275</link>
      <description>&lt;P&gt;Could use pyspark to calculate the recursive logic and save the result as a table?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2024 15:20:58 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/85847#M37275</guid>
      <dc:creator>filipniziol</dc:creator>
      <dc:date>2024-08-28T15:20:58Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/86151#M37294</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/117376"&gt;@filipniziol&lt;/a&gt;&amp;nbsp;, yes i know that is an option, but that would mean i have to do this every time the source table updates, even if the data could possibly not be used before the next update, which is why i wanted to create a view on the data, because as far as i understand, that would calculate the cte at run time and apply filters if any are used.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2024 05:50:09 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/86151#M37294</guid>
      <dc:creator>dlehmann</dc:creator>
      <dc:date>2024-08-29T05:50:09Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/86179#M37296</link>
      <description>&lt;P&gt;In a typical data architecture, data from source systems is ingested into the bronze layer, where it is stored as-is. Subsequent transformations are then applied in the silver layer.&lt;/P&gt;&lt;P&gt;One of these transformations could involve calculating the EmpLevel&amp;nbsp;column. Since recursive CTEs are not supported in Databricks SQL, you have the following options:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Use pyspark&lt;/STRONG&gt;: Use pyspark, as in the example below, and persist the calculated EmpLevel column. This is flexible and you can have as many levels as you need. The table is updated on some basis, so you can consider EmpLevel calculation as one of the steps of this update.&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Hardcode Levels for a Limited Hierarchy&lt;/STRONG&gt;: If the hierarchy depth is known and limited, you can manually define levels using a series of unions. This is for sure not as elegant as recursive CTE or pyspark solution, but it will work as well.&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Thu, 29 Aug 2024 06:34:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/86179#M37296</guid>
      <dc:creator>filipniziol</dc:creator>
      <dc:date>2024-08-29T06:34:12Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/86366#M37314</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/117376"&gt;@filipniziol&lt;/a&gt;&amp;nbsp;, I went with your second suggestion as i preferred to use views in this case. It works very well as there is a limited depth and i could just write that many unions.&lt;/P&gt;&lt;P&gt;Thanks for your response!&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2024 13:11:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/86366#M37314</guid>
      <dc:creator>dlehmann</dc:creator>
      <dc:date>2024-08-29T13:11:55Z</dc:date>
    </item>
    <item>
      <title>Re: Recrusive cte in databrick sql</title>
      <link>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/124491#M47209</link>
      <description>&lt;P&gt;Check this out!&amp;nbsp;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-cte#recursive-query-examples" target="_blank"&gt;https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-cte#recursive-query-examples&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jul 2025 16:44:37 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/recrusive-cte-in-databrick-sql/m-p/124491#M47209</guid>
      <dc:creator>StephanieAlba</dc:creator>
      <dc:date>2025-07-08T16:44:37Z</dc:date>
    </item>
  </channel>
</rss>

