Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save na0fu3y/7eaee4bcd74dac96d37735e8a5456c4b to your computer and use it in GitHub Desktop.
Save na0fu3y/7eaee4bcd74dac96d37735e8a5456c4b to your computer and use it in GitHub Desktop.
BigQuery で 180 日間参照されていないテーブルを出すクエリ
EXECUTE IMMEDIATE(
SELECT
FORMAT("""
WITH
referenced_tables AS(
SELECT
DISTINCT referenced_table.project_id,
dataset_id,
table_id
FROM
`region-us`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
JOIN
UNNEST(referenced_tables)referenced_table),
tables AS(%s)
SELECT
table_catalog,
table_schema,
table_name
FROM
tables
LEFT JOIN
referenced_tables
ON
project_id=table_catalog
AND dataset_id=table_schema
AND table_id=table_name
WHERE
project_id IS NULL
""",STRING_AGG(FORMAT("""
SELECT
table_catalog,
table_schema,
table_name
FROM
`%s`.INFORMATION_SCHEMA.TABLES""",schema_name),"""
UNION ALL"""))
FROM
INFORMATION_SCHEMA.SCHEMATA)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment