arch_db
New Contributor III

@Sardenberg wrote:

This solution seems to have been answered by an AI.
I need to know how create a SELECT for create this tables: USERS, GROUPS and GROUP_USERS.


 

# Databricks notebook source
from pyspark.sql.functions import lit

# COMMAND ----------

df_groups = spark.sql("""SHOW GROUPS""")

# COMMAND ----------

df_users = spark.sql("""SHOW USERS""")

# COMMAND ----------


result = spark.createDataFrame([], "name: string, directGroup: boolean, user: string")
for user in df_users.collect():
    df_user_groups = spark.sql(f"""SHOW GROUPS WITH USER `{user.name}`""").withColumn("user", lit(user.name))
    result = result.unionAll(df_user_groups)

# COMMAND ----------

display(result) # Here you can create temp view and select data

 

It will work fine if groups added to workspace.