我们需要帮助才能在“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
我将不胜感激。
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 )
你可以使用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笔交易。
那么代码会是这样的吗?
SELECT Co_terminal,Co_service,Co_user,COUNT(1)AS NumberOfTransactions FROM YOUR表Group by Co_terminal,Co_service,Co_user,DATEPART(YEAR,Dh_transaction),DATEPART(Month,Dh_transaction),DATEPART(Day,Dh_transaction),DATEPART(Hour,Dh_transaction) )
选择不同的co_terminal(选择t。*,滞后(dh_transaction,9)over(由co_terminal分区,co_service,co_user order by dh_transaction)作为来自TB_TRANSACOES的dh_transaction_9 t)t其中dt_transaction_9> = dh_transaction - interval'1 hour';