Hi, everyone.I have a query that I am so far unable to perform. I need to find the latest row in a child table that corresponds to rows in the parent. The trick is that the latest row is determined by two columns in the row, not just one. Further, I would really like to do this in a single query.If it was one column, I can do it, and will show code below to try to clarify what I am looking for--but two columns is giving me trouble.For example, consider the following tables:create table p ( id int identity primary key, name varchar(50))create table c ( id int identity primary key, p int references p(id), x_dt datetime, y_dt datetime)
If I want to find the latest p for a particular name, determined by a single date in the child c, I can do the following:select p.*, c.*from pjoin c on c.p = p.idjoin ( select max(x_dt) x_dt from c join p on p.id = c.p group by p.name) c2 on c2.x_dt = c.x_dt
However, you'll notice that c has two dates, x_dt and y_dt. What I really need is the latest c determined by the greater of x_dt and y_dt for the row.If you'd like to play with this, here is the full schema script:drop table cdrop table pcreate table p ( id int identity primary key, name varchar(50))create table c ( id int identity primary key, p int references p(id), x_dt datetime, y_dt datetime)insert into p(name)values ('One')insert into p(name)values ('Two')insert into p(name)values ('One')insert into p(name)values ('One')insert into p(name)values ('Two')insert into c(p, x_dt, y_dt)values (1, current_timestamp - 10, current_timestamp - 1)insert into c(p, x_dt, y_dt)values (2, current_timestamp - 8, current_timestamp - 5)insert into c(p, x_dt, y_dt)values (3, current_timestamp - 6, current_timestamp - 2)insert into c(p, x_dt, y_dt)values (4, current_timestamp - 7, current_timestamp - 4)insert into c(p, x_dt, y_dt)values (5, current_timestamp - 9, current_timestamp - 3)If I've done my example correctly, I believe this should return row 1 for name 'One', and row 5 for name 'Two' from p.Thanks for any help!--Kevin