sql - Find missing columns -
i have 2 tables of following structure:
1. city_details: table consists of applicable tables , columns each city.
country_name city_name table_name column_name usa new york t1 t1c1 usa new york t1 t1c2 usa new york t1 t1c3 usa new york t2 t2c1 usa los angeles t2 t2c1 usa los angeles t2 t2c2 usa los angeles t2 t2c3 england london t1 t1c1 england london t3 t3c1 england london t3 t3c2
2. max_column_details: table consists information tables can exist each city , maximum columns each table can have.
table_name column_name t1 t1c1 t1 t1c2 t1 t1c3 t1 t1c4 t2 t2c1 t2 t2c2 t2 t2c3 t2 t2c4 t2 t2c5 t2 t2c6 t3 t3c1 t3 t3c2 t3 t3c3 t4 t4c1 t4 t4c2
i want write sql query returns missing columns tables present in each city. example, new york has t1 3 column t1c1, t1c2, t1c3. t1 can have maximum 4 columns, namely t1c1, t1c2, t1c3, t1c4. so, missing column table t1 in new york t1c4. missing column other tables other cities needed. resultant should under:
country_code city_code table_name column_name usa new york t1 t1c4 usa new york t2 t2c2 usa new york t2 t2c3 usa new york t2 t2c4 usa new york t2 t2c5 usa new york t2 t2c6 usa los angeles t2 t2c4 usa los angeles t2 t2c5 usa los angeles t2 t2c6 england london t1 t1c2 england london t1 t1c3 england london t1 t1c4 england london t3 t3c3
i tried queries using minus, except, exist , not exist, still not able rquired result. not proficient in sql. please help.
with ct (select distinct country_name, city_name, table_name city_details) select ct.country_name, ct.city_name, m.column_name ct, max_column_details m ct.table_name = m.table_name , not exists ( select 1 city_details country_name = ct.country_name , city_name = ct.city_name , table_name = ct.table_name , column_name = m.column_name ) order 1, 2, 3;
should trick
Comments
Post a Comment