cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

SQL CTE in Databricks or something similar?

gbradley145
New Contributor III

%sql

WITH genCTE AS (

SELECT MAX(PredID) + 1 AS PredID, 145 AS SystemID

FROM TableA

UNION ALL

SELECT PredID + 1

FROM genCTE

)

SELECT * FROM genCTE

When I attempt this, I get an error that genCTE does not exists.

There may be a better way to what I am trying to do. Currently in the version of databricks we are running, we cant use an identity column. We are migrating a table and job from SQL Server to databricks. I can do a full load and pull over the identity column. But, I need to continue to populate this identity column going forward.

I also have a tried a SQL UDF where I can easily grab the MAX(ID) and have tried a row_number function over the top of it, but I cant get it work.

Hopefully I have done an adequate job of describing this issue. I simply need continue the IDs and need to do it programmaticly in databricks. I am hoping I can do this via SQL as well as I am not at all familiar with Python, although I would be open to a Python solution, I would just need a little more hand holding with it.

Thank you!

6 REPLIES 6

byrdman
New Contributor III

not seeing anything wrong , but know that your close to making this work.. I do use CTE's in Databricks SQL. make sure your query works as expected outside the CTE. another work around is make the cte portion into a dataframe then create a view from the dataframe, and use it for joins in your sql statement.

PriyaAnanthram
Contributor III

Recursive CTEs I havent yet used I got the below eg working with pyspark code How to implement Recursive Queries in Spark | by Akash Chaurasia | Globant | Medium

Kaniz
Community Manager
Community Manager

Hi @Greg Bradley​ ​, We haven’t heard from you since the last response from @Priya Ananthram​ and @David Byrd​ , and I was checking back to see if you have a resolution yet.

If you have any solution, please share it with the community as it can be helpful to others. Otherwise, we will respond with more details and try to help.

Also, Please don't forget to click on the "Select As Best" button whenever the information provided helps resolve your question.

Hubert-Dudek
Esteemed Contributor III

You are referencing CTE inside of CTE. That's why it is not working. It would be best if you refactored your query.image.png

Kaniz
Community Manager
Community Manager

Hi @Greg Bradley​ ​, We haven’t heard from you since the last response from @Hubert Dudek​ , and I was checking back to see if you have a resolution yet.

If you have any solution, please share it with the community as it can be helpful to others. Otherwise, we will respond with more details and try to help.

Also, Please don't forget to click on the "Select As Best" button whenever the information provided helps resolve your question.

Anonymous
Not applicable

Hi @Greg Bradley​ 

Hope all is well! Just wanted to check in if you were able to resolve your issue and would you be happy to share the solution or mark an answer as best? Else please let us know if you need more help. 

We'd love to hear from you.

Thanks!

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.