我有一个包含以下数据的表,使用 Postgres 9.6:
log_id | 序列 | made_at(时间戳) 206480 1 1 206480 1 2 206480 2 3 206480 3 4 206480 1 5 206480 2 6 206480 4 7 206480 5 8 206480 1 9 206480 2 10 206481 1 11 206481 2 12 206481 3 13 206481 4 14
我必须对 ID 进行分组和聚合,以便获得一系列可能的序列。最后,我希望数据看起来像这样:
log_id | 序列
206480 {1,1,2,3}
206480 {1,2,4,5}
206480{1,2}
206481 {1,2,3,4}
在以下情况下,我想要一个新行(带有序列):
log_id变化; 或者还有另一列指定排序(时间戳),但它在另一个表中(我加入它们并使用该时间戳)。为了使事情更容易,我省略了它,但我们可以假设该列名为made_at.
我很想知道如何创建一个视图,以 10 分钟最接近 10 分钟的间隔分组时间戳,并包含每个最小和最大时间戳。
所以一个看起来像这样的表:
| Hero | timestamp |
| Batman | 2016-12-08 12:00:00 |
| Batman | 2016-12-08 12:07:00 |
| Batman | 2016-12-08 13:00:00 |
| Batman | 2016-12-08 14:00:00 |
| Wonder Woman | 2016-12-08 10:15:00 |
| Wonder Woman | 2016-12-08 10:18:00 |
| Wonder Woman | 2016-12-08 10:25:00 |
| Wonder Woman | 2016-12-08 10:30:00 |
Run Code Online (Sandbox Code Playgroud)
会导致这样的观点
| Hero | start_time | end_time |
| Batman | 2016-12-08 12:00:00 | 2016-12-08 12:07:00 | …Run Code Online (Sandbox Code Playgroud) postgresql group-by gaps-and-islands datetime postgresql-9.5
导入时出现此错误:
Run Code Online (Sandbox Code Playgroud)pg_restore: [archiver (db)] COPY failed for table "transaction_details": ERROR: invalid input syntax for type money: "$0.00"
restore完成,但transaction_details桌子是空的。这是 Heroku 的 PostgreSQL Dump 数据库。
Heroku 上的 PostgreSQL 版本是 9.3.15,在我的系统上也是如此
我使用的命令:
pg_restore: [archiver (db)] COPY failed for table "transaction_details":
ERROR: invalid input syntax for type money: "$0.00"
Run Code Online (Sandbox Code Playgroud)
有人可以提出解决方案吗?
基本上,我试图在 postgresql 中的单个命令中进行多次插入,其中我ON CONFLICT有一个 per-insert 参数。
这是查询(格式为psycopg2):
INSERT INTO
web_pages
(url, starturl, netloc, distance, is_text,
priority, type, addtime, state)
VALUES
(%(url)s, %(starturl)s, %(netloc)s, %(distance)s, %(is_text)s,
%(priority)s, %(type)s, %(addtime)s, %(state)s)
ON CONFLICT (url) DO
UPDATE
SET
state = EXCLUDED.state,
starturl = EXCLUDED.starturl,
netloc = EXCLUDED.netloc,
is_text = EXCLUDED.is_text,
distance = LEAST(EXCLUDED.distance, web_pages.distance),
priority = GREATEST(EXCLUDED.priority, web_pages.priority),
addtime = LEAST(EXCLUDED.addtime, web_pages.addtime)
WHERE
(
web_pages.ignoreuntiltime < %(ignoreuntiltime)s
AND
web_pages.url = EXCLUDED.url
AND
(web_pages.state = 'complete' OR web_pages.state = 'error') …Run Code Online (Sandbox Code Playgroud) 这可能是一个非常简单的问题,但对 Google 来说很难,因为它匹配了太多不同的问题。
我想获取保存在我的数据库中的时间戳的时区(或偏移量)。
例如,现在在我的数据库中,我有
expired_at
---------------------
2018-04-28 00:00:00
2018-03-28 08:00:00
2018-02-28 05:00:00
Run Code Online (Sandbox Code Playgroud)
我想找到所有expired_at具有 PST 偏移量的内容。
所以我的伪代码是这样的
SELECT expired_at FROM table WHERE expired_at IS IN TIMEZONE('PST')
我能得到一些帮助吗。谢谢!
附注。基本上,我试图通过查找所有 UTC 时间戳并将它们更新为 PST 来解决数据完整性问题。数据库现在混杂着许多不同的时区。
是否有任何理由使用 JSONB 值数组而不仅仅是原始 JSONB?
我的用例是我需要在 JSON 对象列表中存储自定义表单定义。我的问题是将其存储为原始 JSON 值还是将其拆分为单独的 JSONB 对象会更好。
每种解决方案的优缺点是什么?
我们有以下数据库结构
CREATE TABLE objects (
id int PRIMARY KEY,
name text,
address text
);
CREATE TABLE tasks (
id int PRIMARY KEY,
object_id int NOT NULL,
actor_id int NOT NULL,
description text
);
CREATE TABLE actors (
id int PRIMARY KEY,
name text
);
Run Code Online (Sandbox Code Playgroud)
用户输入以空格分隔的单词列表(基本上是搜索词),我们必须搜索满足以下条件的任务:如果每个搜索词在任务描述的串联中至少出现一次,则该任务是“匹配”,其关联对象的名称和地址以及关联参与者的名称。
现在,如果我们不关心性能,我们可以这样做(给定查询“foo bar”):
SELECT t.id, t.description
FROM tasks AS t
INNER JOIN actors AS a ON t.actor_id = a.id
INNER JOIN objects AS o ON t.object_id = o.id
WHERE to_tsvector(concat_ws(' ', t.description, o.name, o.address, a.name)) @@ …Run Code Online (Sandbox Code Playgroud) postgresql performance full-text-search postgresql-performance
例如,假设我想为 Chicken Ranch 解析这些地址
Chicken Ranch
10511 Homestead Rd
Pahrump, NV 89061
Chicken Ranch
1600 Pennsylvania Avenue
NW Washington, D.C. 20500
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我都想摆脱Rd和Avenue。例如,在第一种情况下,我想获得“Homestead”,而在第二个“Pennsylvania”中。不过,并非每个地址都有这样的名称。
我们有一个 ERP 系统,它允许使用聚合(例如SUM(foo))但不允许使用 DISTINCT(例如SUM(DISTINCT foo).
是否可以创建一个聚合函数 ( SUM_DISTINCT),它返回与 相同的结果SUM(DISTINCT foo),所以SUM_DISTINCT(foo) = SUM(DISTINCT foo)?
我正在尝试将 Barman 配置为备份。当我做一个barman check replica我不断得到:
Server replica:
WAL archive: FAILED (please make sure WAL shipping is setup)
PostgreSQL: OK
superuser: OK
wal_level: OK
directories: OK
retention policy settings: OK
backup maximum age: FAILED (interval provided: 1 day, latest backup age: No available backups)
compression settings: OK
failed backups: OK (there are 0 failed backups)
minimum redundancy requirements: FAILED (have 0 backups, expected at least 2)
ssh: OK (PostgreSQL server)
not in recovery: FAILED (cannot perform exclusive backup on …Run Code Online (Sandbox Code Playgroud) postgresql ×10
address ×1
aggregate ×1
array ×1
barman ×1
datetime ×1
distinct ×1
group-by ×1
json ×1
money ×1
performance ×1
pg-restore ×1
restore ×1
upsert ×1