Skip to content

Instantly share code, notes, and snippets.

@vithalsamp
Created December 28, 2018 09:10
Show Gist options
  • Save vithalsamp/45693f7a7040df2d91307db76859bfb4 to your computer and use it in GitHub Desktop.
Save vithalsamp/45693f7a7040df2d91307db76859bfb4 to your computer and use it in GitHub Desktop.
SELECT
-- Name of the Table
_V_TABLE.TABLENAME as "Table Name",
-- Column name
SUBSTR(_v_relation_column.attname || ' ', 1, 25) as "Column Name",
-- Statistics Details
CASE WHEN _v_statistic.recent IS NULL THEN 'not maintained '
ELSE
CASE
WHEN _v_statistic.recent = 85 THEN 'Full ' -- Full
WHEN _v_statistic.recent = 341 THEN 'Express ' -- Express
WHEN _v_statistic.recent = 149 THEN 'Basic ' -- No Dispersion
WHEN _v_statistic.recent = 1 THEN 'Full Min/Max OK ' -- Full
WHEN _v_statistic.recent = 257 THEN 'Express Min/Max OK ' -- Express
WHEN _v_statistic.recent = 129 THEN 'Basic Min/Max OK ' -- No Dispersion
WHEN _v_statistic.recent = 0 THEN 'Full Outdated ' -- Full
WHEN _v_statistic.recent = 256 THEN 'Express Outdated ' -- Express
WHEN _v_statistic.recent = 128 THEN 'Basic Outdated ' -- No Dispersion
WHEN _v_statistic.recent = 169 THEN 'Min/Max Only '
WHEN _v_statistic.recent = 170 THEN 'Unavailable '
WHEN _v_statistic.recent = 16554 THEN 'Unavailable '
ELSE SUBSTRING ('flags=' || _v_statistic.recent || ' ', 1, 20)
END
END as "Statistics Status",
-- Minimum Values
SUBSTRING(NVL(_v_statistic.loval,'') || ' ', 1, 15) || ' ' as "Minimum Value",
-- Maximum values in columns
SUBSTRING(NVL(_v_statistic.hival,'') || ' ', 1, 15) as "Maximum Value",
-- Number of Unique Values
CASE WHEN _v_relation_column.attdispersion = 0
THEN ' '
WHEN _v_relation_column.attdispersion = -1
THEN ' 100% Unique '
ELSE TO_CHAR( (CAST((1.0/_v_relation_column.attdispersion) AS BIGINT)), ' 999,999,999,999,999 ' )
END as "# of Unique Values",
-- Number of Null values
CASE WHEN _v_statistic.nullfrac = 0
THEN ' '
ELSE TO_CHAR( (CAST((RELTUPLES * _v_statistic.nullfrac) AS BIGINT)), '999,999,999,999,999' )
END as "# of NULLs"
-- Max Length
, CASE WHEN _v_statistic.maxlen = 0
THEN ''
ELSE TO_CHAR (_v_statistic.maxlen, '99,999') END as "MaxLen"
-- Average Length
, CASE WHEN _v_statistic.sumlen = 0 THEN ''
WHEN _v_statistic.sumlen = 9187201950435737471 THEN ' NaN'
ELSE TO_CHAR (_v_statistic.sumlen / RELTUPLES, '99,999') END as "AvgLen"
FROM _v_relation_column
left outer join _v_statistic on
( _v_relation_column.objid = _v_statistic.objid AND
_v_relation_column.attnum = _v_statistic.attnum
)
inner join _V_TABLE
on (_v_relation_column.objid = _V_TABLE.OBJID)
WHERE
( _v_relation_column.schema=current_schema OR upper(_v_relation_column.schema) in ('DEFINITION_SCHEMA', 'INFORMATION_SCHEMA'))
ORDER BY
_V_TABLE.TABLENAME ,
_v_relation_column.attnum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment