红移或任何SQL:使用时间在30秒内的类似行更新表

红移或任何SQL:使用时间在30秒内的类似行更新表,sql,amazon-redshift,Sql,Amazon Redshift,我有这张桌子: prod | customer | city | num | time | isextra -----+----------+---------+------+--------------------+------- 1 | Jim | Venice | 5 |2015-08-27 1:10:00 | 0 1 | Jim | Venice | 5 |2015-08-27 1:10:15 | 0

我有这张桌子:

prod | customer |   city  | num  |       time         | isextra
-----+----------+---------+------+--------------------+-------
 1   | Jim      |  Venice |  5   |2015-08-27 1:10:00  | 0
 1   | Jim      |  Venice |  5   |2015-08-27 1:10:15  | 0
 1   | Jim      |  Venice |  5   |2015-08-27 1:10:28  | 0
 4   | Jane     |  Vienna |  8   |2018-06-04 2:20:43  | 0
 4   | Jane     |  Vienna |  8   |2018-06-04 2:20:43  | 0
 4   | Jane     |  Vienna |  8   |2018-06-04 2:20:49  | 0
 4   | Jane     |  Vienna |  8   |2018-06-04 2:30:55  | 0
 7   | Jack     | Vilnius |  4   |2015-09-15 2:20:55  | 0
 7   | Jake     |   Vigo  |  9   |2018-01-01 10:20:05 | 0
 7   | Jake     |   Vigo  |  2   |2018-01-01 10:20:25 | 0
现在,取所有按prod、customer、city、num相似的行,然后取组中第一行时间在30秒内的任何行,其“isextra”字段更新为1,结果如下:

prod | customer |   city  | num  |       time         | isextra
-----+----------+---------+------+--------------------+-------
 1   | Jim      |  Venice |  5   |2015-08-27 1:10:00  | 0
 1   | Jim      |  Venice |  5   |2015-08-27 1:10:15  | 1
 1   | Jim      |  Venice |  5   |2015-08-27 1:10:28  | 1
 4   | Jane     |  Vienna |  8   |2018-06-04 2:20:43  | 0
 4   | Jane     |  Vienna |  8   |2018-06-04 2:20:43  | 1
 4   | Jane     |  Vienna |  8   |2018-06-04 2:20:49  | 1
 4   | Jane     |  Vienna |  8   |2018-06-04 2:30:55  | 0
 7   | Jack     | Vilnius |  4   |2015-09-15 2:20:55  | 0
 7   | Jake     |   Vigo  |  9   |2018-01-01 10:20:05 | 0
 7   | Jake     |   Vigo  |  2   |2018-01-01 10:20:25 | 0
以下是表格和数据:

create table mytable (prod int, customer varchar, city varchar, num int, time timestamp, isextra smallint);


insert into mytable values (1, 'Jim', 'Venice', 5, '2015-08-27 1:10:00',  0);
insert into mytable values (1, 'Jim', 'Venice',  5, '2015-08-27 1:10:15',  0);
insert into mytable values (1, 'Jim', 'Venice',  5, '2015-08-27 1:10:28',  0);
insert into mytable values (4, 'Jane',  'Vienna',   8,   '2018-06-04 2:20:43',   0);
insert into mytable values (4, 'Jane',  'Vienna',   8,   '2018-06-04 2:20:43',   0);
insert into mytable values (4, 'Jane',  'Vienna',   8,   '2018-06-04 2:20:49',   0);
insert into mytable values (4, 'Jane',  'Vienna',   8,   '2018-06-04 2:30:55',   0);
insert into mytable values (7, 'Jack', 'Vilnius',   4,   '2015-09-15 2:20:55',   0);
insert into mytable values (7, 'Jake',   'Vigo',    9,   '2018-01-01 10:20:05',  0);
insert into mytable values (7, 'Jake',   'Vigo',    2,   '2018-01-01 10:20:25',  0);
到目前为止,我只知道:

UPDATE mytable
SET isextra = 1
FROM ( 
  select *, 
  row_number() over (partition by prod, customer, city, num order by time asc)
    as t from mytable
) AS sequence
困在这里


任何想法,谢谢

不知道这是否会在PostgreSQL 8.0(红移)中出现,但值得一试:

update mytable a
  set isextra = 1 
  from (
    select prod, customer, city, num, min(time) as mintime
      from mytable 
      group by prod, customer, city, num
  ) b
  where a.prod = b.prod
    and a.customer = b.customer
    and a.city = b.city
    and a.num = b.num
    and a.time <= b.mintime + interval '30 seconds'
    and a.time <> b.mintime;

我将使用如下窗口函数编写一个
选择

select t.*,
       (case when time > min_time and
                  time < dateadd(minute, 30, min_time)
             then 1 else 0 
        end) as is_extra
from (select t.*,
             min(time) over (partition by prod, customer, city, num) as min_time
      from t
     ) t;

标记您正在使用的DBMS(即
MySQL
SQL Server
,等等)。谢谢,除非我需要更新。@hulungpu。你有完全重复的行,这使得这很难做到。是的,我这样做是为了在出现这些行时尝试删除它们。你最好启动一个事务,创建一个临时表,保存结果,删除匹配的记录并插入新记录。谢谢,这还不完全存在,它将除一行之外的所有isextra设置为1。我想这次我得到了它。请复习。是的,就是这样!!多谢各位@呼伦布。对于重复的行,这是如何得到正确答案的?我不认为是这样。事实上,我不知道这是否真的可能,因为没有办法区分它们。
select t.*,
       (case when time > min_time and
                  time < dateadd(minute, 30, min_time)
             then 1 else 0 
        end) as is_extra
from (select t.*,
             min(time) over (partition by prod, customer, city, num) as min_time
      from t
     ) t;
select t.*,
       (case when time > min_time and time < dateadd(minute, 30, min_time) and seqnum <> 0
             then 1 else 0 
        end) as is_extra
from (select t.*,
             min(time) over (partition by prod, customer, city, num) as min_time,
             row_number() over (partition by prod, customer, city, num order by time) as seqnum
      from t
     ) t;
update t
    set t.is_extra = tt.new_is_extra
    from (select t.*,
                 (case when time > min_time and time < dateadd(minute, 30, min_time) and seqnum <> 0
             then 1 else 0 
                  end) as new_is_extra
          from (select t.*,
                       min(time) over (partition by prod, customer, city, num) as min_time,
                       row_number() over (partition by prod, customer, city, num order by time) as seqnum
                from t
                ) t
         ) tt
     where t.id = tt.id