'How to get the partition column names for a table?

I have a table that is partitioned on one or more columns. I can do ...

SHOW PARTITIONS table_db.table_1

which gives a list of all partitions like this,

year=2007
year=2015
year=1999
year=1993

but I am only interested in finding which columns the table is partitioned on, in this case, year. And I would like to be able to do this of multiple tables at once, giving me a list of their names and partitioned columns somewhat like this.

table_name       partition_col
table_1          year
table_2          year, month

I tried the solutions here...

https://docs.aws.amazon.com/athena/latest/ug/querying-glue-catalog.html#querying-glue-catalog-listing-partitions

SELECT * FROM table_db."table_1$partitions"

does give me results with one column for each partition...

#     year
1     2007
2     2015
3     1999
4     1993

...but I couldn't extract the column names from this query.



Solution 1:[1]

Try this.

SELECT table_name,
    array_join(array_agg(column_name), ', ') as partition_col
FROM information_schema.columns
WHERE extra_info = 'partition key'
GROUP BY 1

Solution 2:[2]

it seems solution is for mysql not SQL Server.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 esimonov
Solution 2 Qaiser Qaiser