집계 함수가 있는 쿼리 - Amazon Timestream

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

집계 함수가 있는 쿼리

다음은 집계 함수를 사용하여 쿼리를 설명하는 IoT 시나리오 예제 데이터 세트의 예입니다.

예시 데이터

Timestream을 사용하면 하나 이상의 트럭 플릿의 위치, 연료 소비, 속도 및 부하 용량과 같은 IoT 센서 데이터를 저장하고 분석하여 효과적인 플릿 관리를 가능하게 할 수 있습니다. 다음은 트럭의 위치, 연료 소비, 속도 및 적재 용량과 같은 원격 측정을 저장하는 테이블 iot_trucks의 스키마 및 일부 데이터입니다.

Time 트럭_id Make 모델 플릿 fuel_capacity, 연료_용량 load_capacity measure_name measure_value::double measure_value::varchar

2019-12-04 19:00:00.000000000

123456781

GMC

아스트로

Alpha(알파)

100

500

fuel_reading

65.2

null

2019-12-04 19:00:00.000000000

123456781

GMC

아스트로

Alpha(알파)

100

500

로드

400.0

null

2019-12-04 19:00:00.000000000

123456781

GMC

아스트로

Alpha(알파)

100

500

속도

90.2

null

2019-12-04 19:00:00.000000000

123456781

GMC

아스트로

Alpha(알파)

100

500

location

null

47.6062 N, 122.3321 W

2019-12-04 19:00:00.000000000

123456782

Kenworth

W900

Alpha(알파)

150

1000

fuel_reading

10.1

null

2019-12-04 19:00:00.000000000

123456782

Kenworth

W900

Alpha(알파)

150

1000

로드

950.3

null

2019-12-04 19:00:00.000000000

123456782

Kenworth

W900

Alpha(알파)

150

1000

속도

50.8

null

2019-12-04 19:00:00.000000000

123456782

Kenworth

W900

Alpha(알파)

150

1000

location

null

40.7128도 N, 74.0060도 W

쿼리 예제

플릿의 각 트럭에 대해 모니터링되는 모든 센서 속성 및 값의 목록을 가져옵니다.

SELECT truck_id, fleet, fuel_capacity, model, load_capacity, make, measure_name FROM "sampleDB".IoT GROUP BY truck_id, fleet, fuel_capacity, model, load_capacity, make, measure_name

지난 24시간 동안 플릿의 각 트럭에 대한 최신 연료 판독값을 얻습니다.

WITH latest_recorded_time AS ( SELECT truck_id, max(time) as latest_time FROM "sampleDB".IoT WHERE measure_name = 'fuel-reading' AND time >= ago(24h) GROUP BY truck_id ) SELECT b.truck_id, b.fleet, b.make, b.model, b.time, b.measure_value::double as last_reported_fuel_reading FROM latest_recorded_time a INNER JOIN "sampleDB".IoT b ON a.truck_id = b.truck_id AND b.time = a.latest_time WHERE b.measure_name = 'fuel-reading' AND b.time > ago(24h) ORDER BY b.truck_id

지난 48시간 동안 연료 부족(10% 미만)으로 실행된 트럭을 식별합니다.

WITH low_fuel_trucks AS ( SELECT time, truck_id, fleet, make, model, (measure_value::double/cast(fuel_capacity as double)*100) AS fuel_pct FROM "sampleDB".IoT WHERE time >= ago(48h) AND (measure_value::double/cast(fuel_capacity as double)*100) < 10 AND measure_name = 'fuel-reading' ), other_trucks AS ( SELECT time, truck_id, (measure_value::double/cast(fuel_capacity as double)*100) as remaining_fuel FROM "sampleDB".IoT WHERE time >= ago(48h) AND truck_id IN (SELECT truck_id FROM low_fuel_trucks) AND (measure_value::double/cast(fuel_capacity as double)*100) >= 10 AND measure_name = 'fuel-reading' ), trucks_that_refuelled AS ( SELECT a.truck_id FROM low_fuel_trucks a JOIN other_trucks b ON a.truck_id = b.truck_id AND b.time >= a.time ) SELECT DISTINCT truck_id, fleet, make, model, fuel_pct FROM low_fuel_trucks WHERE truck_id NOT IN ( SELECT truck_id FROM trucks_that_refuelled )

지난 주에 각 트럭의 평균 부하 및 최대 속도를 찾습니다.

SELECT bin(time, 1d) as binned_time, fleet, truck_id, make, model, AVG( CASE WHEN measure_name = 'load' THEN measure_value::double ELSE NULL END ) AS avg_load_tons, MAX( CASE WHEN measure_name = 'speed' THEN measure_value::double ELSE NULL END ) AS max_speed_mph FROM "sampleDB".IoT WHERE time >= ago(7d) AND measure_name IN ('load', 'speed') GROUP BY fleet, truck_id, make, model, bin(time, 1d) ORDER BY truck_id

지난 한 주 동안 각 트럭에 대한 로드 효율성을 얻습니다.

WITH average_load_per_truck AS ( SELECT truck_id, avg(measure_value::double) AS avg_load FROM "sampleDB".IoT WHERE measure_name = 'load' AND time >= ago(7d) GROUP BY truck_id, fleet, load_capacity, make, model ), truck_load_efficiency AS ( SELECT a.truck_id, fleet, load_capacity, make, model, avg_load, measure_value::double, time, (measure_value::double*100)/avg_load as load_efficiency -- , approx_percentile(avg_load_pct, DOUBLE '0.9') FROM "sampleDB".IoT a JOIN average_load_per_truck b ON a.truck_id = b.truck_id WHERE a.measure_name = 'load' ) SELECT truck_id, time, load_efficiency FROM truck_load_efficiency ORDER BY truck_id, time