0

我们每天都将新记录插入到一​​个表中,其中包含 id 和 created_on 列。

如何确定过去 7 天内是否每天都存在具有特定标识符的记录?

4

2 回答 2

1

这可以通过存储过程来完成:

CREATE OR REPLACE PROCEDURE TIME_TRAVEL(QUERY TEXT, DAYS FLOAT)
RETURNS VARIANT LANGUAGE JAVASCRIPT AS
$$
  function run_query(query, offset) {
    try {
      var sqlText = query.replace('"at"', " AT(OFFSET => " + (offset + 0) + ") ");
      return (snowflake.execute({sqlText: sqlText})).next();
    }
    catch(e) { return false }
  }
  var days, result = [];
  for (days = 0; days < DAYS; days++)
    if (run_query(QUERY, -days * 86400)) result.push(days);
  return result;
$$;

CALL TIME_TRAVEL('SELECT * FROM TASK_HISTORY "at" WHERE QUERY_ID = ''019024ef-002e-8f71-0000-05e10030a782''', 7);

要使时间旅行查询替换工作,请将 an"at"作为表别名。
返回值是查询返回任何值时的日期偏移量数组。
只有DAYS=2当您拥有 Snowflake Enterprise Edition 时,这才有效。

于 2019-11-27T08:15:58.777 回答
0

我用下面的查询做到了

select id,  sum(present) as total_count 
from
    (select id,feed_date, count(1) as present 
    from catalog_rca 
    where feed_date between '2019-11-19' and '2019-11-25'  
    group by 1,2) as temp
group by 1 having total_count = 7;
于 2019-11-27T10:33:16.713 回答