2

我有一个名为asset_usages 的表,它记录了查看者对资产的查看。相关领域是

id (int)
asset_id (int)
viewer_type (string)
viewer_id (int)
viewed_at (datetime)

我刚刚添加了一个名为 time_between_viewings 的新字段,它是一个表示秒数的 int 字段。我想将其设置为自上次查看该资产以来的时间(以秒为单位)。所以,如果我有这四个记录:

+-----+----------+-----------+-------------+---------------------+-----------------------+
| id  | asset_id | viewer_id | viewer_type | viewed_at           | time_between_viewings |
+-----+----------+-----------+-------------+---------------------+-----------------------+
| 506 |     7342 |      1182 | User        | 2009-01-05 11:10:01 |      NULL             |
| 509 |     7342 |      1182 | User        | 2009-01-05 11:12:47 |      NULL             |
| 514 |     6185 |      1182 | User        | 2009-01-05 11:14:28 |      NULL             |
| 524 |     6185 |      1182 | User        | 2009-01-05 11:28:18 |      NULL             |
| 618 |     1234 |      1182 | User        | 2009-01-05 11:29:03 |      NULL             |
| 729 |     1234 |      1182 | User        | 2009-01-05 11:29:01 |      NULL             |        
+-----+----------+-----------+-------------+---------------------+-----------------------+

那么 time_between_viewings 应该设置如下:

+-----+----------+-----------+-------------+---------------------+-----------------------+
| id  | asset_id | viewer_id | viewer_type | viewed_at           | time_between_viewings |
+-----+----------+-----------+-------------+---------------------+-----------------------+
| 506 |     7342 |      1182 | User        | 2009-01-05 11:10:01 |      NULL             |
| 509 |     7342 |      1182 | User        | 2009-01-05 11:12:47 |      166              |
| 514 |     6185 |      1182 | User        | 2009-01-05 11:14:28 |      NULL             |
| 524 |     6185 |      1182 | User        | 2009-01-05 11:28:18 |      830              |
| 618 |     1234 |      1182 | User        | 2009-01-05 11:29:03 |      2                |
| 729 |     1234 |      1182 | User        | 2009-01-05 11:29:01 |      NULL             |     
+-----+----------+-----------+-------------+---------------------+-----------------------+

其中 166 和 830 是每对之间的时间差,以秒为单位。

填充该字段的 sql 是什么?我不太明白。

重要提示:数据并不总是按时间顺序插入到数据库中。即,您可以有两条记录 A 和 B,其中 B 具有更高的 id,但 A 具有较晚的值 view_at。因此,查找具有较低 id 的第一个匹配记录不一定会给您之前由同一个人查看的记录 - 您需要检查数据库中的所有记录。

谢谢!最大限度

编辑 - 声明 time_between_viewings 是一个表示秒的 int 字段。

编辑 - 添加了几行作为具有更高 id 但更早时间戳的行的示例

编辑 - 我刚刚意识到我没有正确规定这个问题。time_between_viewings 应该等于自同一查看器上次查看资产以来的时间,即记录与具有相同asset_id、viewer_id 和viewer_type 的上一个(基于viewed_at)记录之间的时间。我提供的示例数据仍然有效,但我可以输入一些不同的 viewer_id 和 viewer_type 值来充实示例。

4

2 回答 2

0

此 SELECT 语句将为您提供正确的数据。不过,您可能需要分块进行更新。

您可以删除 UPDATE 语句的 ORDER BY 子句。正如所写,派生数据不依赖于外部 SELECT 语句中的行顺序。

select asset_id, viewer_id, viewer_type, viewed_at, 
       prev_viewed_at, timestampdiff(second, prev_viewed_at, viewed_at) elapsed_sec
from (select asset_id, viewer_id, viewer_type, viewed_at,
            (select max(t2.viewed_at) 
             from Table1 t2
             where t2.viewed_at < Table1.viewed_at
               and t2.asset_id = Table1.asset_id
               and t2.viewer_id = Table1.viewer_id
            ) prev_viewed_at
      from Table1
      )t3
order by asset_id, viewer_id, viewed_at;
于 2013-07-17T12:05:16.660 回答
0

如果您准备了示例表和数据插入,这将很有帮助。
如果您想获得帮助,请阅读此链接以了解为什么它如此重要:http: //tkyte.blogspot.com/2005/06/how-to-ask-questions.html

这次我为您创建了它,点击这个链接:http ://sqlfiddle.com/#!2/9719a/2

并尝试此查询(您将在上述链接下找到此查询以及示例数据):

select alias1.*,
       timestampdiff( second, previous_viewed_at, viewed_at ) 
         as time_between_viewings
from (
select alias.*,
       (
         select viewed_at from (
             select
             ( select count(*) from asset_usages y
                where x.asset_id = y.asset_id 
                and y.viewed_at < x.viewed_at
              ) as rn,
              x.* 
              from asset_usages x
          ) xyz
          where xyz.asset_id = alias.asset_id 
               and xyz.rn = alias.rn - 1
       ) previous_viewed_at
from (
  select
    ( select count(*) from asset_usages y
      where x.asset_id = y.asset_id 
        and y.viewed_at < x.viewed_at
    ) as rn,
    x.* 
  from asset_usages x
) alias
) alias1;
于 2013-07-16T16:43:12.997 回答