cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
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
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
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! 
 

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.