【发布时间】:2025-12-05 10:15:01
【问题描述】:
PostgreSQL 9.4.5
我每天晚上 11:55 运行以下 select 语句。我想运行基于硬编码的 start_date 和 end_date 循环通过以下查询的每月查询(或函数)。查询必须每天处理结果。最后,我想看看我们在给定的一个月里有多少迷失的羊。
目的:Lost Sheep- 我们正在寻找尝试登录 MySoftware 并且一整天都没有成功的用户。
*The IP address is compared against other login attempts that may be successful the rest of the day. In the event of a login failure, the query should look into the ip address and verify that no successful logins happened from that same IP address within the day.
SELECT DISTINCT l.username, l.ip
FROM login l
WHERE
l.ip NOT IN
(SELECT l.ip
FROM login l
WHERE
date_trunc('day', l."time") = current_date
AND l.succeeded = 'TRUE')
AND date_trunc('day', l."time") = current_date
AND l.succeeded = 'False'
fields in Table logins
1. id -int
2. username --character varying(255)
3. ip -- character varying (255)
4. time -- timestamp without time zone
5. succeeded -- boolean
【问题讨论】:
标签: postgresql datetime