Skip to content
Advertisement

Redshift – CASE statement checking column EXISTS or no

I am querying dynamically tables where some of the tables might not have specific column. My intention is check the existence of the column and dynamically assign a value. Basically if all the tables would contain the field I would just write simply :

But in my case I need to do something like this:

I am getting the error:

SQL Error [500310] [0A000]: Amazon Invalid operation: Specified types or functions (one per INFO message) not supported on Redshift tables.;

Advertisement

Answer

The following is a trick that works on most databases to handle missing columns.

pk is the primary key column for the table. This uses scoping rules to find a value for k_val. If k_val is in the table, then the subquery will use the value from that row. If not, then the scope will “reach out” and take the value from k. There is no confusion in this case, because k_val is not in tbl.

If you don’t want a constant subquery for some reason, you can always use:

You can then use this as a subquery or CTE for your aggregation purposes.

Having said all that, I am wary of handling missing columns this way.

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