从历史记录重建表

Pau*_*aul 5 postgresql recovery

我的 PostgreSQL 8.4 数据库发生了一场小灾难。

我在两个表中跟踪访问者访问我的网页:

历史

+---------------+-------------+
| whereseenlast | varchar(50) |
| uniqid        | varchar(13) |
| whenseenlast  | timestamp   |
| bla1          | varchar(10) |
| bla2          | varchar(10) |
| bla3          | varchar(10) |
+---------------+-------------+
Run Code Online (Sandbox Code Playgroud)

样本数据:

+-----------------+---+---------------------+----------+----------+----------+
| where1.html     | 1 | 2000-01-01 00:00:00 | somebla1 | somebla2 | somebla3 |
| where2.html     | 1 | 2005-05-05 05:06:07 | somebla1 | somebla2 | somebla3 |
| where12345.html | 1 | 2012-11-22 11:22:33 | somebla1 | somebla2 | somebla3 |
+-----------------+---+---------------------+----------+----------+----------+
Run Code Online (Sandbox Code Playgroud)

访客

uniqid 是主键,每个访问者只能在此表中找到一次

+---------------+----------------+
| wherseenfirst | varchar(50)    |
| whereseenlast | varchar(50)    |
| whenseenfirst | timestamp      |
| whenseenlast  | timestamp,     |
| uniqid        | varchar(13) PK |
| notes         | varchar(20)    |
| xyz           | varchar(20)    |
+---------------+----------------+
Run Code Online (Sandbox Code Playgroud)

样本数据:

+-------------+-----------------+----------------- ----+---------------------+---+-----------+-------- --+
| where1.html | where12345.html | 2000-01-01 00:00:00 | 2012-11-22 11:22:33 | 1 | 备注 | 查看更多
+-------------+-----------------+----------------- ----+---------------------+---+-----------+-------- --+

问题

我不小心删除了访客表,我没有备份。但我相信专家很容易通过查询从历史记录重建访问者表。我知道我将无法恢复笔记xyz列,但这没什么大不了的。

请问有什么建议吗?

ara*_*nid 0

就像是:

select uniqid,
       min(case when visit_seq = 1 then whenseenlast end) as whenseenfirst,
       min(case when visit_seq = 1 then whereseenlast end) as whereseenfirst,
       min(case when visit_seq = visits_thisuser then whenseenlast end) as whenseenlast,
       min(case when visit_seq = visits_thisuser then whereseenlast end) as whereseenlast
from (
  select uniqid,
         row_number() over(w) as visit_seq,
         count(*) over(w range between unbounded preceding and unbounded following) as visits_thisuser,
         whenseenlast, whereseenlast
  from history
  window w as (partition by uniqid order by whenseenlast)
) visit
group by visit.uniqid
Run Code Online (Sandbox Code Playgroud)

http://sqlfiddle.com/#!11/fb6f8/14/0