查询在SQL postgres中选择

时间:2017-05-10 15:21:59

标签: sql postgresql

我们需要帮助才能在" SELECT"中使用命令。从PostgreSQL中的银行返回以下信息。

"在1小时内发生10次或更多次的终端,具有相同的用户,服务和终端"。

我们有表 TB_TRANSACOES

Id bigserial NOT NULL,
Dh_transaction timestamp NOT NULL,
Nu_account bigint NOT NULL,
Nu_value bigint NOT NULL,
Co_terminal character varying NOT NULL,
Co_service character varying NOT NULL,
Co_user character varying NOT NULL,
CONSTRAINT tb_transacoes_pkey PRIMARY KEY (id)

我们只有这一部分:

SELECT * FROM TB_TRANSACOES WHERE CO_TERMINAL >= 10

我很感激。

3 个答案:

答案 0 :(得分:0)

所有带有NumberOfTransactions>的记录10应该是你想要的

SELECT 
    Co_terminal 
    , Co_service 
    ,Co_user 
    , COUNT(1) AS NumberOfTransactions
FROM YOUR table
Group by 
    Co_terminal 
    , Co_service 
    ,Co_user 
    ,DATEPART(YEAR, Dh_transaction )
    ,DATEPART(Month, Dh_transaction )
    ,DATEPART(Day, Dh_transaction )
    ,DATEPART(Hour, Dh_transaction )

答案 1 :(得分:0)

您可以使用lag()

执行此操作
select distinct co_terminal
from (select t.*,
             lag(dh_transaction, 9) over (partition by co_terminal, co_service, co_user
                                          order by dh_transaction
                                         ) as dh_transaction_9
      from TB_TRANSACOES t
     ) t
where dt_transaction_9 >= dh_transaction - interval '1 hour';

这是历史上第9次交易。如果是在一小时内,那么我们在一小时内就有10笔交易。

答案 2 :(得分:0)

那么代码会是这样吗?

选择     Co_terminal     ,Co_service     ,Co_user     ,COUNT(1)AS NumberOfTransactions 从你的桌子 通过...分组     Co_terminal     ,Co_service     ,Co_user     ,DATEPART(年,Dh_transaction)     ,DATEPART(月,Dh_transaction)     ,DATEPART(Day,Dh_transaction)     ,DATEPART(小时,Dh_transaction)

选择不同的co_terminal 从(选择t。*,              滞后(dh_transaction,9)结束(由co_terminal,co_service,co_user分区                                           按dh_transaction排序                                          )作为dh_transaction_9       来自TB_TRANSACOES t      )t 其中dt_transaction_9> = dh_transaction - 间隔'1小时';