PostgresSQL - 循环每日Select语句,用于月末报告

时间:2015-10-27 14:45:41

标签: postgresql datetime

PostgreSQL 9.4.5

以下选择声明我每天晚上11:55运行。我想运行每月查询(或函数),它基于硬编码的start_date和end_date循环通过以下查询。查询必须每天处理结果。最后,我想看看我们在一个月内失去了多少只绵羊。

目的:失去的绵羊 - 我们正在寻找尝试登录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

1 个答案:

答案 0 :(得分:0)

select date_trunc('day', l.time) as 'day', l.username, l.ip
from login l
where date_trunc('month', l.time) = '2015-09-01'
group by 1, 2, 3
having not bool_or(succeded)