cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Use output of SHOW PARTITION commands in Sub-Query/CTE/Function

pshah83
New Contributor II

I am using SHOW PARTITIONS <<table_name>> to get all the partitions of a table. I want to use max() on the output of this command to get the latest partition for the table.

However, I am not able to use SHOW PARTITIONS <<table_name>> in a CTE/sub-query/table function. All of them fail with syntax error. Any inputs

with t as (
SHOW PARTITIONS <<table_name>>
)
select * from t

OR

select * from (
SHOW PARTITIONS <<table_name>>
)

2 REPLIES 2

Kaniz_Fatma
Community Manager
Community Manager

Hi @pshah83, In Databricks SQL, you can’t use SHOW PARTITIONS in a subquery or CTE as it’s not supported. However, you can use a workaround to get the latest partition of a table.

First, run the SHOW PARTITIONS <<table_name>> command in Databricks SQL. This will return a table with a single column called partition.

Then, you can use the max() function on the partition column to get the latest partition.

Kaniz_Fatma
Community Manager
Community Manager

Thank you for posting your question in our community! We are happy to assist you.

To help us provide you with the most accurate information, could you please take a moment to review the responses and select the one that best answers your question?

This will also help other community members who may have similar questions in the future. Thank you for your participation and let us know if you need any further assistance! 
 

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!