0

I have two possibilities to get data from two different tables: Table A and Table B.

The table B doesn't have one column(ColumnD), so I wanna to keep the full schema

I tried using Python(Spark):

#input argument
table_argument = 'B'

spark.sql(f'''
SELECT 
 ColumnA,
 ColumnB,
 ColumnC,
 CASE
   WHEN '{table_argument}' = 'B' THEN NULL
 ELSE ColumnD 
 END AS ColumnD
FROM {table_argument}''')

I got always the ERROR that is not possible to get the ColumnD. The When condition didn't work.

Is there any possibility to do this?

Thanks

1 Answer 1

1

You can't reference a nonexistent column, even in a WHEN clause that's not executed. You need to use a Python condition to construct that part of the query.

spark.sql(f'''
SELECT 
 ColumnA,
 ColumnB,
 ColumnC,
 {'NULL' if table_argument == 'B' else 'ColumnD'} AS ColumnD
FROM {table_argument}''')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.