A test that uses spark-sql
is failing due to the following error:
Correlated scalar subqueries must be Aggregated
The relevant part of the SQL query that is failing:
COALESCE(
CASE WHEN ndc_code IN
(SELECT CODE FROM BOC_MED_METADATA WHERE CODE_TYPE = 'ndc')
THEN '180'
END,
CASE WHEN hcpcs_code IN
(SELECT CODE FROM BOC_MED_METADATA WHERE CODE_TYPE = 'hcpcs')
THEN '180'
END,
med_order_end_dttm,
CASE WHEN days_supply IS NOT NULL
THEN
CASE
WHEN discontinued AND (med_order_recorded_dttm BETWEEN medication_start AND start_plus)
THEN med_order_recorded_dttm
WHEN discontinued AND (med_order_recorded_dttm > start_plus)
THEN start_plus
WHEN NOT discontinued
THEN start_plus
END
END,
medication_start
)
The first two cases
in the coalesce
are what I added and caused the test to fail. I believe it is because of how the subqueries will return multiple rows but I'm not sure how to get around that. Any help is appreciated. If it makes a difference, this SQL
is only run in spark for the tests, it is actually run on a snowflake warehouse in production.