

# 날짜 및 시간을 사용한 쿼리
<a name="query-examples-waf-logs-date-time"></a>

이 섹션의 예제에는 날짜 및 시간 값을 사용하는 쿼리가 포함되어 있습니다.
+ [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 - 사람이 읽을 수 있는 ISO 8601 형식으로 타임스탬프 필드 반환**  
다음 쿼리는 `from_unixtime` 및 `to_iso8601` 함수를 사용하여 `timestamp` 필드를 사람이 읽을 수 있는 ISO 8601 형식으로 반환합니다(예: `1576280412771`이 아닌 `2019-12-13T23:40:12.000Z`). 쿼리는 HTTP 소스 이름, 소스 ID, 요청도 반환합니다.  

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

**Example - 최근 24시간의 레코드 반환**  
다음 쿼리는 `WHERE` 절에 필터를 사용해 최근 24시간의 레코드의 HTTP 소스 이름, HTTP 소스 ID, HTTP 요청 필드를 반환합니다.  

```
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 - 지정된 날짜 범위 및 IP 주소에 대한 레코드 반환**  
다음 쿼리는 지정된 클라이언트 IP 주소에 대해 지정된 날짜 범위의 레코드를 나열합니다.  

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

**Example - 지정된 날짜 범위에 대해 5분 간격으로 IP 주소 개수 계산**  
다음 쿼리는 특정 날짜 범위에 대해 5분 간격으로 IP 주소 개수를 셉니다.  

```
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 - 지난 10일 동안 X-Forwarded-For IP 수 계산**  
다음 쿼리는 요청 헤더를 필터링하고 지난 10일 동안의 X-Forwarded-For IP 수를 계산합니다.  

```
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
```

날짜 및 시간 함수에 대한 자세한 내용은 Trino 설명서의 [날짜 및 시간 함수와 연산자](https://trino.io/docs/current/functions/datetime.html)를 참조하세요.