

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Data e ora interrogare utilizzando data e ora
<a name="query-examples-waf-logs-date-time"></a>

Gli esempi di questa sezione illustrano le query che utilizzano i valori di data e ora.
+ [Return the timestamp field in human-readable ISO 8601 format](#waf-example-return-human-readable-timestamp)
+ [Return records from the last 24 hours](#waf-example-return-records-last-24-hours)
+ [Return records for a specified date range and IP address](#waf-example-return-records-date-range-and-ip)
+ [For a specified date range, count the number of IP addresses in five minute intervals](#waf-example-count-ip-addresses-in-date-range)
+ [Count the number of X-Forwarded-For IP in the last 10 days](#waf-example-count-x-forwarded-for-ip)

**Example : Restituisci il campo timestamp in formato ISO 8601 leggibile dall'uomo**  
La seguente query utilizza le funzioni `from_unixtime` e `to_iso8601` per restituire il campo `timestamp` in formato ISO 8601 leggibile dalle persone (ad esempio, `2019-12-13T23:40:12.000Z` invece di `1576280412771`). La query restituisce anche il nome dell'origine HTTP, l'ID di origine e la richiesta.   

```
SELECT to_iso8601(from_unixtime(timestamp / 1000)) as time_ISO_8601,
       httpsourcename,
       httpsourceid,
       httprequest
FROM waf_logs
LIMIT 10;
```

**Example : Restituisci record delle ultime 24 ore**  
La query seguente utilizza un filtro nella clausola `WHERE` per restituire il nome dell'origine HTTP, l'ID origine HTTP e i campi di richiesta HTTP per i registri delle ultime 24 ore.  

```
SELECT to_iso8601(from_unixtime(timestamp/1000)) AS time_ISO_8601, 
       httpsourcename, 
       httpsourceid, 
       httprequest 
FROM waf_logs
WHERE from_unixtime(timestamp/1000) > now() - interval '1' day
LIMIT 10;
```

**Example : Restituisci i record per un intervallo di date e un indirizzo IP specificati**  
Nella query seguente sono elencati i registri in un intervallo di date specificato per un indirizzo IP client specificato.  

```
SELECT * 
FROM waf_logs 
WHERE httprequest.clientip='53.21.198.66' AND "date" >= '2021/03/01' AND "date" < '2021/03/31'
```

**Example : per un intervallo di date specificato, conta il numero di indirizzi IP in intervalli di cinque minuti**  
La query seguente conta, per un determinato intervallo di date, il numero di indirizzi IP in intervalli di cinque minuti.  

```
WITH test_dataset AS 
  (SELECT 
     format_datetime(from_unixtime((timestamp/1000) - ((minute(from_unixtime(timestamp / 1000))%5) * 60)),'yyyy-MM-dd HH:mm') AS five_minutes_ts,
     "httprequest"."clientip" 
     FROM waf_logs 
     WHERE "date" >= '2021/03/01' AND "date" < '2021/03/31')
SELECT five_minutes_ts,"clientip",count(*) ip_count 
FROM test_dataset 
GROUP BY five_minutes_ts,"clientip"
```

**Example — Conta il numero di X-Forwarded-For IP negli ultimi 10 giorni**  
La seguente query filtra le intestazioni delle richieste e conta il numero di X-Forwarded-For IP negli ultimi 10 giorni.  

```
WITH test_dataset AS
  (SELECT header
   FROM waf_logs
   CROSS JOIN UNNEST (httprequest.headers) AS t(header)
   WHERE from_unixtime("timestamp"/1000) > now() - interval '10' DAY) 
SELECT header.value AS ip,
       count(*) AS COUNT 
FROM test_dataset 
WHERE header.name='X-Forwarded-For' 
GROUP BY header.value 
ORDER BY COUNT DESC
```

Per ulteriori informazioni sulle funzioni data e ora, consulta [Funzioni e operatori di data e ora](https://trino.io/docs/current/functions/datetime.html) nella documentazione Trino.