select distinct foo.id key, last_value(data.value) over ( partition by data.foo_id order by data.id ) value from foo, foo_data data where foo.id <> ? and foo.activity is true — and data.foo_id = foo.id and data.aktivity is true and data.key = 'name';