我需要按日期/时间字段对PostgreSQL表进行排序,例如last_updated.
但是,该字段允许为空或空,我想与空记录last_updated来之前,非空last_updated.
这可能吗?
order by last_updated asc /* and null last_updated records first ?? */
Run Code Online (Sandbox Code Playgroud) 我在PostgreSQL中有以下查询:
SELECT
COUNT(a.log_id) AS overall_count
FROM
"Log" as a,
"License" as b
WHERE
a.license_id=7
AND
a.license_id=b.license_id
AND
b.limit_call > overall_count
GROUP BY
a.license_id;
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误:
错误:列"overall_count"不存在
我的表结构:
License(license_id, license_name, limit_call, create_date, expire_date)
Log(log_id, license_id, log, call_date)
Run Code Online (Sandbox Code Playgroud)
我想检查许可证是否已达到特定月份的通话限制.
我有一个工作查询,按硬件模型和结果对数据进行分组,但问题是有很多"结果".我试图将其减少到"如果结果= 0然后保持为0,否则将其设置为1".这通常有效,但我最终得到:
day | name | type | case | count
------------+----------------+------+------+-------
2013-11-06 | modelA | 1 | 0 | 972
2013-11-06 | modelA | 1 | 1 | 42
2013-11-06 | modelA | 1 | 1 | 2
2013-11-06 | modelA | 1 | 1 | 11
2013-11-06 | modelB | 1 | 0 | 456
2013-11-06 | modelB | 1 | 1 | 16
2013-11-06 | modelB | 1 | 1 | 8
2013-11-06 …Run Code Online (Sandbox Code Playgroud) 我正在从 Postgres 表中聚合数据,查询大约需要 2 秒,我想将其减少到不到一秒。
请在下面找到执行细节:
询问
select
a.search_keyword,
hll_cardinality( hll_union_agg(a.users) ):: int as user_count,
hll_cardinality( hll_union_agg(a.sessions) ):: int as session_count,
sum(a.total) as keyword_count
from
rollup_day a
where
a.created_date between '2018-09-01' and '2019-09-30'
and a.tenant_id = '62850a62-19ac-477d-9cd7-837f3d716885'
group by
a.search_keyword
order by
session_count desc
limit 100;
Run Code Online (Sandbox Code Playgroud)
表元数据
查询计划
Custom Scan (cost=0.00..0.00 rows=0 width=0) (actual time=1722.685..1722.694 rows=100 loops=1)
Task Count: 1
Tasks Shown: All
-> Task
Node: host=localhost port=5454 dbname=postgres
-> Limit (cost=64250.24..64250.49 …Run Code Online (Sandbox Code Playgroud) sql postgresql indexing query-performance postgresql-performance