小编Dav*_*veB的帖子

选择最长的连续序列

我正在尝试在 PostgreSQL 9.0 中构建一个查询,该查询获取特定列的最长连续行序列。

考虑下表:

lap_id (serial), lap_no (int), car_type (enum), race_id (int FK)
Run Code Online (Sandbox Code Playgroud)

lap_no每个(race_id, car_type). where都是独一无二的。

我希望查询为给定的race_idand生成最长的序列car_type,因此它将返回int最高的(或长的)。

使用以下数据:

1, 1, red, 1
2, 2, red, 1
3, 3, red, 1
4, 4, red, 1
5, 1, blue, 1
6, 5, red, 1
7, 2, blue, 1
8, 1, green, 1
Run Code Online (Sandbox Code Playgroud)

对于car_type = red and race_id = 1查询将5作为lap_no字段的最长序列返回。

我在这里发现了一个类似的问题但是我的情况更简单一些。

(我也想知道car_type …

postgresql window-functions gaps-and-islands postgresql-9.0

12
推荐指数
2
解决办法
9110
查看次数

Postgres - 窗口函数排名和计数

我有一些表格可以跟踪运动员在赛道上的时间:

竞赛、竞赛用户和竞赛用户会话

我正在构建一个查询,用于提取每个提供的“competition_user”的当前排名和竞争对手总数,我可以得到排名,但是我的计数 (totalUsers) 没有计算竞争中的所有竞争对手,它似乎只是向上计数给提供的用户(例如,与排名相同)

SELECT compUserId, rank, totalUsers 
    FROM (
        SELECT cu.competition_user_id as compUserId, cu.user_id as userId,  
    count(*) OVER w as totalUsers, rank() OVER w as rank 
        FROM competition_users cu 
        LEFT JOIN current_competition_sessions ccs ON cu.competition_user_id = ccs.competition_user_id 
        LEFT JOIN competition_user_sessions cus ON cus.competition_user_session_id = ccs.competition_user_session_id 
        WHERE cu.left_competition = false 
        AND cu.competition_id in (:compIds)
        WINDOW w AS (PARTITION BY cu.competition_id ORDER BY cus.time_in_seconds ASC) 
    ) as sub 
WHERE compUserId in (:compUserIds)
Run Code Online (Sandbox Code Playgroud)

我的理解是默认框架是整个窗口,而这似乎是从框架开始到当前行计数?

postgresql window-functions

8
推荐指数
1
解决办法
1万
查看次数

AWS RDS PostgreSQL 转储/恢复 - 语法错误

我正在尝试将 postgresql 数据库从 EC2 实例导入到同一子网上的 RDS,到目前为止,我正在执行以下操作...

1) 转储 EC2 数据库:

pg_dump --host localhost --port 5432 -Fc --encoding='UTF8' -U postgres ProcessorDB > /home/jboss/proc_dump_jan15.dump
Run Code Online (Sandbox Code Playgroud)

2) 从 EC2 恢复:

psql -f /home/jboss/proc_dump_jan15.dump --host=blahrds.11938475.eu-west-1.rds.amazonaws.com --port=5432 --username=postgres --password --dbname=ProcessorDB
Run Code Online (Sandbox Code Playgroud)

我能够连接到远程数据库,但出现以下错误:

psql:/home/jboss/proc_dump_jan15.dump:1: ERROR:  syntax error at or near "PGDMP"
LINE 1: PGDMP
             REVOKE ALL ON SCHEMA public FROM postgres;
        ^
GRANT
GRANT
psql:/home/jboss/proc_dump_jan15.dump:5: ERROR:  syntax error at or near ""
LINE 1:     'en'
        ^
psql:/home/jboss/proc_dump_jan15.dump:14: ERROR:  syntax error at or near ""
LINE 1:     'free',
        ^ …
Run Code Online (Sandbox Code Playgroud)

postgresql aws postgresql-9.3 amazon-rds

2
推荐指数
1
解决办法
7424
查看次数