Anonymous
Not applicable

@ELENI GEORGOUSI​ : Can you try using a common table expression (CTE) something like below?

WITH cte_total_qty AS (
  SELECT SUM(qty) AS total_qty
  FROM my_schema.my_staging_source_table
  WHERE YEAR(ModifiedDate) = ${var.year_variable} AND CostCenter = $CostCenter
)
 
Select col1, col2,...
from....

Note that the CTE is executed only once, before the main SQL statement, and the result is reused for each row in the main SQL statement. Hope this helps!

View solution in original post