October 22, 2024
Chicago 12, Melborne City, USA
SQL

Big query multi-statement with a declare


I’m trying to UNION ALL requests (one request by quarter).
I want to save this request as a view however I can’t because the declare of date array return a first result and so I’ve multi-statement (I only want to have the second result).
I don’t understand why.
Can you help me please ?

DECLARE i INT64 DEFAULT 1;
DECLARE dsql STRING DEFAULT '';
DECLARE rollingdate ARRAY<DATE> DEFAULT GENERATE_DATE_ARRAY("2024-01-07", DATE_ADD(CURRENT_DATE(),INTERVAL 1 QUARTER), INTERVAL 1 QUARTER);
WHILE i <= array_length(rollingdate)
DO
  SET dsql = dsql || " SELECT *, '" || rollingdate[ORDINAL(i)] || "' AS flash_date FROM table WHERE table.date="" || rollingdate[ORDINAL(i)] || "" union all";
  SET i = i + 1;
END WHILE;
SET dsql = SUBSTR(dsql, 1, LENGTH(dsql) - LENGTH(' union all'));
EXECUTE IMMEDIATE dsql;

results



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video