Snowflake-使用TimeDiff的相关子查询更新



我在雪花数据库上运行此查询:

UPDATE "click" c
SET "Registration_score" =
(SELECT COUNT(*) FROM "trackingpoint" t
WHERE 1=1
AND c."CookieID" = t."CookieID"
AND t."page" ilike '%Registration complete'
AND TIMEDIFF(minute,c."Timestamp",t."Timestamp") < 4320
AND TIMEDIFF(second,c."Timestamp",t."Timestamp") > 0);

数据库返回Unsupported subquery type cannot be evaluated。但是,如果我在没有最后两个条件的情况下运行它(带有计时夫),则可以毫无问题地运行。我确认实际的计时声明对这些疑问还可以:

select count(*) from "trackingpoint"
where TIMEDIFF(minute, '2018-01-01', "Timestamp") > 604233;
select count(*) from "click"
where TIMEDIFF(minute, '2018-01-01', "Timestamp") > 604233;

这些工作没有问题。我看不到定时夫条件避开数据库返回结果的原因。知道我应该改变什么才能使它起作用?

,因此使用以下设置

create table click (id number, 
   timestamp timestamp_ntz,
   cookieid number,
   Registration_score number);
create table trackingpoint(id number, 
   timestamp timestamp_ntz, 
   cookieid number, 
   page text );

insert into click values (1,'2018-03-20', 101, 0),
    (2,'2019-03-20', 102, 0);
insert into trackingpoint values (1,'2018-03-20 00:00:10', 101, 'user reg comp'),
    (2,'2018-03-20 00:00:11', 102, 'user reg comp'),
    (3,'2018-03-20 00:00:13', 102, 'pet reg comp'),
    (4,'2018-03-20 00:00:15', 102, 'happy dance');

您可以看到我们得到了我们期望的行

select c.*, t.*
from click c
join trackingpoint t 
    on c.cookieid = t.cookieid ;

现在有两种方法可以获取您的计数,这是您拥有的第一个方法,如果您只计数一件事,那就很好,因为所有规则都是加入过滤:

select c.id,
  count(1) as new_score
from click c
join trackingpoint t 
    on c.cookieid = t.cookieid
    and t.page ilike '%reg comp'
    and TIMEDIFF(minute, c.timestamp, t.timestamp) < 4320
group by 1;

或者您可以(在雪花语法中)将计数移至汇总/选择侧,因此如果您需要的话,请获得多个答案(我发现自己更多的地方,因此为什么我会介绍它):

select c.id,
    sum(iff(t.page ilike '%reg comp' AND TIMEDIFF(minute, c.timestamp, t.timestamp) < 4320, 1, 0)) as new_score
from click c
join trackingpoint t 
    on c.cookieid = t.cookieid
group by 1;

因此将其插入更新模式(请参阅文档的最后一个示例)https://docs.snowflake.net/manuals/sql-reference/sql/update.html

您可以移至单个子选择,而不是雪花不支持的统一子查询,这是您收到的错误消息。

UPDATE click c
SET Registration_score = s.new_score
from (
    select ic.id,
        count(*) as new_score
    from click ic
    join trackingpoint it 
        on ic.cookieid = it.cookieid
        and it.page ilike '%reg comp'
        and TIMEDIFF(minute, ic.timestamp, it.timestamp) < 4320
    group by 1) as s
WHERE c.id = s.id; 

添加时间介绍的原因将您的查询变成相关的子问题,是更新的每一行,现在与子查询结果(相关性)相关。围绕的工作是使"大但更简单"的子查询并加入。

相关内容

  • 没有找到相关文章

最新更新