$query = OrderTable::query()
->registerRuntimeField('LOG', array(
'data_type'=> LogTable::class,
'reference' => [
'=this.ID' => 'ref.ORDER_ID',
]
))
->registerRuntimeField('LAST_LOG_ENTITY', [
'data_type'=>'integer',
'expression' => ['MAX(%s)', 'LOG.ID']
])
->where(
Query::filter()
->logic('or')
->whereNull('LAST_LOG_ENTITY')
->where('LOG.ID', '=', 'LAST_LOG_ENTITY')
)
;