Search code examples
sqlamazon-redshift

select column 'name' or 'nm' from table 'tb2' only 1 column will exist at a time either 'name' or 'nm'


in amazon redshift table i need to select column from table tb2 following conditions:

  • table refresh everyday so it will either have column 'name' or 'nm'
  • if name is present then value of name should be there
  • if nm is present then value of nm should be there
  • both column will not exist together in any condition

pg_get_late_data_columns and information schema not working for me


Solution

  • This will need to be some form of dynamic SQL. You should be able to get the info you need in pg_table_def - https://docs.aws.amazon.com/redshift/latest/dg/r_PG_TABLE_DEF.html