-- Common code audit checks
SELECT code_group, code_value
FROM common_codes
WHERE code_group LIKE '% %';

SELECT code_group, code_value
FROM common_codes
WHERE code_value LIKE '% %';

SELECT code_group, code_value, LEN(code_group) AS code_group_len, LEN(code_value) AS code_value_len
FROM common_codes
WHERE LEN(code_group) > 80
   OR LEN(code_value) > 120
   OR LEN(code_name) > 200;

SELECT code_group, COUNT(*)
FROM common_codes
GROUP BY code_group
ORDER BY code_group;

SELECT DISTINCT c.service_type
FROM clients c
LEFT JOIN common_codes cc
  ON cc.code_group = 'CLIENT_SERVICE_TYPE'
 AND cc.code_value = c.service_type
WHERE c.service_type IS NOT NULL
  AND cc.code_value IS NULL;

SELECT c.status, COUNT(*) AS cnt
FROM clients c
LEFT JOIN common_codes cc
  ON cc.code_group = 'CLIENT_STATUS'
 AND cc.code_value = c.status
WHERE c.status IS NOT NULL
  AND cc.code_value IS NULL
GROUP BY c.status;
