34 lines
1.5 KiB
SQL
34 lines
1.5 KiB
SQL
WITH normalized_tags AS (
|
|
SELECT
|
|
memo.id,
|
|
memo.history_id,
|
|
memo.user_id,
|
|
regexp_replace(trim(memo.tag), '[[:space:]]+', ' ', 'g') AS clean_tag,
|
|
lower(regexp_replace(trim(memo.tag), '[[:space:]]+', ' ', 'g')) AS clean_key,
|
|
row_number() OVER (
|
|
PARTITION BY memo.history_id,
|
|
lower(regexp_replace(trim(memo.tag), '[[:space:]]+', ' ', 'g'))
|
|
ORDER BY memo.created_at, memo.id
|
|
) AS duplicate_rank
|
|
FROM public.memo_tags AS memo
|
|
), memo_impact AS (
|
|
SELECT
|
|
count(*)::bigint AS total,
|
|
count(*) FILTER (WHERE clean_key = '' OR char_length(clean_tag) > 80)::bigint AS invalid,
|
|
count(*) FILTER (WHERE duplicate_rank > 1)::bigint AS duplicates
|
|
FROM normalized_tags
|
|
), owner_impact AS (
|
|
SELECT count(*)::bigint AS mismatches
|
|
FROM public.memo_tags AS memo
|
|
JOIN public.history AS history_item ON history_item.id = memo.history_id
|
|
WHERE memo.user_id <> history_item.user_id
|
|
)
|
|
SELECT
|
|
(SELECT total FROM memo_impact) AS memo_tags_total,
|
|
(SELECT invalid FROM memo_impact) AS memo_tags_invalid_to_delete,
|
|
(SELECT duplicates FROM memo_impact) AS memo_tags_duplicates_to_delete,
|
|
(SELECT mismatches FROM owner_impact) AS memo_tag_owner_repairs,
|
|
(SELECT count(*)::bigint FROM public.meetings) AS meetings_total,
|
|
(SELECT count(*)::bigint FROM public.processing_jobs) AS processing_jobs_total,
|
|
(SELECT count(*)::bigint FROM public.profiles) AS profiles_total,
|
|
(SELECT count(*)::bigint FROM public.subscriptions) AS subscriptions_total;
|