I'm looking for a SQL Server query to find the number of distinct values in a column of a table (cardinality of columns) using database schema metadata. I have only database metadata and cannot query the table.
I have this query but it directly query the table. I want to get the same information by querying database schema metadata.
DECLARE @TableName VARCHAR(MAX) = 'tablename';
DECLARE @SQL NVARCHAR(MAX) = '';
SELECT
@SQL += 'SELECT ''' + COLUMN_NAME + ''' AS ColumnName,
COUNT(DISTINCT [' + COLUMN_NAME + ']) AS Cardinality
FROM [' + TABLE_SCHEMA + '].[' + @TableName + ']
UNION ALL '
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = @TableName;
SET @SQL = LEFT(@SQL, LEN(@SQL) - 10)
EXEC sp_executesql @SQL;
You can use the statistics histogram, if it exists, for the column. You would need a single-column statistics object with just that column as the key.
You need the right permissions for this to work. The docs say
Note that table statistics are not perfectly reliable, especially if a full scan has not been done recently.