# Queries
Esperando atualização
SELECT account_id, sku, action, target, created_at
FROM (
SELECT account_id, sku, action, target, created_at
, RANK() OVER(PARTITION BY account_id, sku, action ORDER BY created_at DESC) rank
FROM `lunar-sled-217521.skyhub.timeline_metrics`
)
WHERE rank=1 and target = "skyhub"
Last B2w Update
SELECT * FROM (
SELECT
account_id, sku, action, target, created_at,
RANK() OVER(PARTITION BY account_id, sku, action ORDER BY created_at DESC) rank
FROM (
SELECT
t1.account_id, t1.sku, t1.action, t1.target, t1.created_at
FROM
`lunar-sled-217521.skyhub.timeline_metrics` as t1
JOIN
`lunar-sled-217521.skyhub.timeline_metrics_waiting_update` as t2
ON
t1.account_id = t2.account_id and t1.sku = t2.sku and t1.action = t2.action
WHERE
t1.target = 'b2w'
)
) WHERE rank=1
LIST all first updates wating to go to b2w
SELECT
account_id, sku, action, target, created_at
FROM (
SELECT
t3.account_id, t3.sku, t3.action, t3.target, t3.created_at,
RANK() OVER(PARTITION BY t3.account_id, t3.sku, t3.action ORDER BY t3.created_at ASC) rank
FROM
`lunar-sled-217521.skyhub.timeline_metrics` t3
JOIN
`lunar-sled-217521.skyhub.timeline_metrics_last_b2w_updates` as t4
ON
t3.account_id = t4.account_id and t3.sku = t4.sku and t3.action = t4.action
WHERE
t3.target = 'skyhub' and
t3.created_at > t4.created_at
)
WHERE
rank=1
LIST all pending updates from first update
SELECT
account_id, sku, action, target, created_at,
TIMESTAMP_DIFF(CURRENT_TIMESTAMP(), created_at, SECOND) as seconds_opening
FROM (
SELECT
account_id, sku, action, target, created_at,
RANK() OVER(PARTITION BY account_id, sku, action ORDER BY created_at ASC) rank
FROM (
SELECT
account_id, sku, action, target, created_at
FROM
`lunar-sled-217521.skyhub.timeline_metrics_waiting_update_first_insert`
UNION ALL
SELECT
account_id, sku, action, target, created_at
FROM `lunar-sled-217521.skyhub.timeline_metrics_waiting_update`
)
) WHERE rank = 1
INSERT HELPER
INSERT INTO `lunar-sled-217521.skyhub.timeline_metrics`
(account_id, sku, action, target, created_at, inserted_at)
VALUES ("account_id_2", "sku_2", "price", "skyhub", CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP())
SELECT
account_id,
sku,
action,
target,
created_at,
TIMESTAMP_DIFF(CURRENT_TIMESTAMP(), created_at, SECOND) as seconds_opening
FROM (
SELECT
account_id,
sku,
action,
target,
created_at,
RANK() OVER(PARTITION BY account_id, sku, action ORDER BY created_at DESC) rank
FROM `table_name`
)
WHERE rank=1 and target = "skyhub"