2017-05-10 1 views
-2

Wir brauchen Hilfe, um einen Befehl in "SELECT" zu verwenden, um die folgenden Informationen von einer Bank in Postgree SLQ zurückzugeben.Abfrage Wählen Sie in SQL nach

"Terminals mit 10 oder mehr Vorkommen, innerhalb von 1 Stunde, mit dem gleichen Benutzer, Service und Terminal".

Wir haben den Tisch 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) 

Wir haben nur diesen Teil:

SELECT * FROM TB_TRANSACOES WHERE CO_TERMINAL> = 10 

ich dankbar sein werde.

Antwort

0

Alle Datensätze mit NumberOfTransactions> 10 sollte sein, was Sie wollen

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) 
+0

Ruft dieser Code die Ereignisse auch im 1-Stunden-Intervall auf? Vielen Dank! –

0

Sie können diese lag() mit tun:

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'; 

Dies wird die neunte Transaktion in der Geschichte. Wenn es innerhalb einer Stunde ist, haben wir 10 Transaktionen innerhalb einer Stunde.

0

Würde der Code dann so aussehen?

SELECT Co_terminal , Co_service , Co_user , COUNT (1) als NumberOfTransactions vom Tisch Gruppe von Co_terminal , Co_service , Co_user , DATEPART (YEAR, Dh_transaction) , DATEPART (Monat, Dh_transaction) , DATEPART (Tag, Dh_transaction) , DATEPART (Stunde, Dh_transaction)

select distinct co_terminal fr om (wähle t *, liegt (dh_transaction, 9) über (Partition durch co_terminal, co_service, co_user Sortieren nach dh_transaction ) als dh_transaction_9 von TB_TRANSACOES t .) t wo dt_transaction_9> = dh_transaction - Intervall '1 Stunde' ;