R
R
RiantHoff2014-09-12 05:57:27
Oracle
RiantHoff, 2014-09-12 05:57:27

Is there a ready-made function in PL/SQL (Oracle SQL) to select a value in field1 from multiple existing rows in a table that differ in the date in field2?

Oracle database.
PL/SQL language.
Only write select queries are allowed.
The table_name table has rows ranging from zero to several rows of the form:

key		field_name	date_field		person_key
---		---		---			---
3		Thom		01.01.1947		43464
7		Emma		02.02.2001		43464
5		Elza		07.07.1980		43464
4		Nelson		03.03.1955		32543

Suppose these are the names of relatives with dates of birth.
You need to select the name of the youngest relative of person 43464. If there are no relatives, then issue "noname", for example.
Is there some standard function that would give the name?
function_name(table_name, field_name, date_field, 43464) = "Emma"
And if not, which query is more efficient (least time-consuming) to get the result?
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Skahin, 2014-09-12
@pihel

Read about KEEP (DENSE_RANK FIRST ORDER BY date_field ASC) or OVER (PARTITION BY person_key ORDER BY date_field)

A
Alexander Lebedev, 2014-09-12
@cawaleb

As far as I understand the question, you need something like this:
SQL> select * from cawa.qqq where PERSON_KEY=43464 and DATE_FIELD=( select max (DATE_FIELD) from cawa.qqq where PERSON_KEY=43464 );
KEY FIELD_NAME DATE_FIELD PERSON_KEY
---------------------------------------------------- ----------------------- ----------------------
7 Emma 02-02-2001 00:00:00 43464

R
RiantHoff, 2014-09-12
@RiantHoff

And it's even better to get the result of the form for all persons at once:
person_key, field_name
43464, "Emma"
32543, "Nelson"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question