I have the task of finding all unique data types in all the tables.
This is how I wrote the SQL query:
WITH UniqueColumnTypes AS
(
SELECT table_name, COUNT(DISTINCT data_type) AS unique_types
FROM information_schema.columns
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
GROUP BY table_name
)
SELECT table_name, unique_types
FROM UniqueColumnTypes;
For some reason, CHAR(13)
and CHAR(12)
are considered two different types.
I need them to be considered as the same data type.
I check the data types of the table with this code:
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'Book' AND table_schema = 'Stud';
I can see, that there are only 4 unique data types in the Book table. But the answer that the first code is giving me is 6 unique data types.
This is how this table is being created:
CREATE TABLE Stud.Book
(
ISBN CHAR(13) NOT NULL,
Title VARCHAR(32) NOT NULL,
Publisher CHAR(12) NOT NULL,
Year SMALLINT NOT NULL
CONSTRAINT LeidimoMetai CHECK (Year > 1900 AND Year < EXTRACT(YEAR FROM CURRENT_DATE) + 1),
Pages SMALLINT NOT NULL
CONSTRAINT PagesCount CHECK (Pages > 0 AND Pages < 2000),
Price DECIMAL(5, 2)
CONSTRAINT PriceSize CHECK (Price > 0 AND Price < 1000),
PRIMARY KEY (ISBN)
);
With this, I can see, that there are only 4 unique data types in that table.
2
Answers
It looks like that the issue that is happening is due to the inclusion of character codes (CHAR) with different lengths as distinct data types. In your case, CHAR(13) and CHAR(12) are being treated as separate data
To handle the case where different character lengths are considered as distinct data types, you can modify your SQL query to eliminate the distinction based on length. Here’s an example:
WITH UniqueColumnTypes AS ( SELECT table_name, CASE WHEN data_type LIKE 'CHAR%' THEN 'CHAR' ELSE data_type END AS unique_type FROM information_schema.columns WHERE table_schema NOT IN ('pg_catalog', 'information_schema') GROUP BY table_name, unique_type ) SELECT table_name, COUNT(DISTINCT unique_type) AS unique_types FROM UniqueColumnTypes GROUP BY table_name;
in this modified query, the CASE expression is used to check if the data type starts with ‘CHAR’. If it does, it assigns the value ‘CHAR’ to the unique_type column. This ensures that all character data types are considered as one unique data type, regardless of their length.
By grouping the UniqueColumnTypes subquery by both table_name and unique_type, and then performing the final grouping and counting in the outer query, you can achieve the desired result of finding the unique data types in each table.
You need to add
As the size is not part of the datatype
fiddle