Skip to content
Advertisement

SQL: How to determine the most frequent data length in the DB column?

Need to analyze values’ length in DB column and get the % of the Number of the values with the same length.

Desire result:

It’s not a ‘find most frequent value and calculate its length’, because if we have a KEY or ID column with high cardinality – all values will be different.

Need some fast-working SQL (DB2 dialect is prefered) – not to overload the DB engine (billions of rows).

Example 1

Result:

Example 2

Result:

Advertisement

Answer

A single SELECT statement using GROUP BY GROUPING SETS operator for any number of columns. The example below presumes, that the constants are result of the corresponding length(varchar_col).

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement