where('trafficinfo_uid', $uid) ->orderBy('created_at', 'DESC') ->first(); // 첫 번째 레코드(=가장 최신) } public function get95PercentileValue(TrafficEntity $trafficEntity, string $field, string $startDate, string $endDate) { $sql = "WITH ranked AS ( SELECT `{$field}`,`created_at`, ROW_NUMBER() OVER (ORDER BY `{$field}` DESC) AS rn,COUNT(*) OVER () AS total FROM {$this->table} WHERE trafficinfo_uid = ? AND created_at BETWEEN ? AND ? ) SELECT `{$field}` FROM ranked WHERE rn = FLOOR(total * 0.05) LIMIT 1"; $result = $this->query($sql, [ $trafficEntity->getPK(), $startDate, $endDate ]); log_message('debug', '[95PERCENTILE SQL] :' . $this->db->getLastQuery()); return $result->getRow()->$field ?? null; } }