小编Jun*_*ooq的帖子

获取昨天从 PostgreSQL 开始到结束的记录

目前我的应用程序正在运行超过 2 个 dbs,其中snapshotDB一个是evercamDB。我们snapshotDB正在保存记录,现在它想要检索它们,当前正在使用此查询

select camera_id,count(*) as snapshot_count 
from snapshots 
where snapshot_id in (select snapshot_id 
                      from snapshots 
                      where created_at = 'now'::date - 1) 
group by camera_id
Run Code Online (Sandbox Code Playgroud)

我想要的是获取snapshots今天正在考虑的记录2016-01-21,并且我想获取昨天的所有记录。'2016-01-20 00:00:00''2016-01-20 23:59:59''now'::date - 1我真的不知道如何以我所写的格式绑定时间。

我想输入这个日期和时间,(select snapshot_id from snapshots where created_at = 'now'::date - 1)以便我可以获得我想要的记录。

任何建议或帮助将不胜感激

postgresql-9.3

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

在 where 子句中使用子查询列

这个问题已经被问过几次,甚至也得到了回答,但这对我没有帮助,这就是为什么我再次发布它。我有这个查询:

select u.*,l.*, 
(
    select count(cr.id) 
    from cloud_recordings cr 
    left join cameras c 
        on c.owner_id=u.id 
    where c.id=cr.camera_id
) valid_licence
from users u 
left join licences l 
    on l.user_id=u.id;
Run Code Online (Sandbox Code Playgroud)

它工作正常,但是当我在最后放一个 where 子句时:

select u.*,l.*, 
(
    select count(cr.id) 
    from cloud_recordings cr 
    left join cameras c 
        on c.owner_id=u.id 
    where c.id=cr.camera_id
) valid_licence
from users u 
left join licences l 
    on l.user_id=u.id
where valid_licence > 0;
Run Code Online (Sandbox Code Playgroud)

它只是给了我一个错误:

错误:“valid_licence”列不存在第
4 行:在 l.user_id=u.id 上留下加入许可 l,其中 valid_licence >..

postgresql

4
推荐指数
1
解决办法
3749
查看次数

获取昨天的记录,但每小时

我目前正在从我的数据库中获取昨天的所有数据,但我想获取每小时的数据。这是我的查询:

select
    camera_id,
    count(*) as snapshot_count
from snapshots
where created_at >= TIMESTAMP 'yesterday' 
  and created_at <  TIMESTAMP 'today'
group by camera_id
Run Code Online (Sandbox Code Playgroud)

它给了我昨天的所有记录,但是怎么可能从昨天获取记录但每个小时?

PS:我想在一个查询中运行它,而不是 24 个查询

postgresql postgresql-9.3

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

标签 统计

postgresql ×2

postgresql-9.3 ×2