I have created a scheduled query that returns a count of users, and transactions on each day. Here is the code:
SELECT
event_date,
COUNT(DISTINCT user_id) users,
COUNT(DISTINCT transaction_id) transactions,
FROM `xyz.events`
WHERE
event_date = current_date
GROUP BY event_date
ORDER BY event_date
The query shown above works when I execute it manually. But when I use it as a scheduled query it doesn't update the destination table as it should even though if I check the runs, it shows that the query has run successfully for that particular day.
The query shown below however does the trick and runs exactly as intended. It updates the daily count of users and transactions in the destination table.
SELECT
DATE_SUB(@run_date, INTERVAL 1 DAY) event_date,
COUNT(DISTINCT user_id) users,
COUNT(DISTINCT transaction_id) transactions,
FROM `xyz.events`
WHERE
event_date = DATE_SUB(@run_date, INTERVAL 1 DAY)
GROUP BY event_date
ORDER BY event_date
So I wanted to understand why this is happening? Because when run manually both the queries give the same output.