November 30, 2005 at 3:55 am
Hi,
I have two relating tables joined on id. The child table can have none, one or more records relating to the parent table, and contains a recency date column.
How can i join the tables and only select the most recent record from the child table?
I don't really want to write a function to do this. I was hoping to be able to do it all in one SELECT statement.
Help!!
November 30, 2005 at 4:47 am
You may need to give us more information, but based upon what your post states, this should work...
CREATE TABLE #Parent( IdentityField integer,
OtherField varchar(15))
INSERT INTO #Parent
VALUES( 1, 'First Value')
INSERT INTO #Parent
VALUES( 2, 'Second Value')
CREATE TABLE #Child( IdentityField integer,
ForeignKey integer,
RecencyDate smalldatetime)
INSERT INTO #Child
VALUES( 1, 1, '01/01/2005')
INSERT INTO #Child
VALUES( 2, 1, '07/13/2005')
INSERT INTO #Child
VALUES( 3, 1, '12/31/2005')
INSERT INTO #Child
VALUES( 4, 2, '01/01/2005')
INSERT INTO #Child
VALUES( 5, 2, '07/13/2005')
INSERT INTO #Child
VALUES( 6, 2, '12/31/2005')
INSERT INTO #Child
VALUES( 7, 2, '01/01/2006')
SELECT P.IdentityField AS Parent_IdentityField,
C.ForeignKey AS Child_ForeignKey,
P.OtherField AS Parent_OtherField,
C.MaxRecencyDate AS Child_MaxRecencyDate
FROM #Parent P
INNER JOIN( SELECT ForeignKey, MAX( RecencyDate) AS MaxRecencyDate FROM #Child GROUP BY ForeignKey) C
ON( P.IdentityField = C.ForeignKey)
DROP TABLE #Parent
DROP TABLE #Child
I wasn't born stupid - I had to study.
November 30, 2005 at 5:09 am
Thank Farrell!
That's on the right tracks, but not quite what i need - I should of explained more.
I have a table of company names and a child table of previous names. A company can have no previous name or many previous names. I need to make a selection of company name and most recent previous name. If the company has no previous name, then I just want a NULL value in previous name. I've modified your example and included the result set I want to see. I just don't know how to achieve it.
OtherField varchar(15))
INSERT INTO #Parent
VALUES( 1, 'First Value')
INSERT INTO #Parent
VALUES( 2, 'Second Value')
VALUES( 3, 'Third Value')
ForeignKey integer,
RecencyDate smalldatetime,
INSERT INTO #Child
VALUES( 1, 1, '01/01/2005', 'A')
INSERT INTO #Child
VALUES( 2, 1, '07/13/2005', 'B')
INSERT INTO #Child
VALUES( 3, 1, '12/31/2005', 'C')
INSERT INTO #Child
VALUES( 4, 2, '01/01/2005', 'D')
INSERT INTO #Child
VALUES( 5, 2, '07/13/2005', 'E')
INSERT INTO #Child
VALUES( 6, 2, '12/31/2005', 'F')
INSERT INTO #Child
VALUES( 7, 2, '01/01/2006', 'G')
What i need to see is this:
2 2 Second Value 2006-01-01 00:00:00 G
Hope this makes sense.
November 30, 2005 at 5:57 am
select * from #Parent p join #Child c on p.IdentityField = c.ForeignKey and recencydate =
(select max(recencydate) from #Child d where d.ForeignKey= p.IdentityField)
I hope this will do...
November 30, 2005 at 7:35 am
That works! Thanks grasshopper!!
Much appreciated.
November 30, 2005 at 8:03 am
Really I don't think so
You stated that a parent may be present but no child yet you still wanted to show the parent
inner is the default for join and therefore will only show parent where there is a least one child
change the join to a LEFT OUTER JOIN
Far away is close at hand in the images of elsewhere.
Anon.
November 30, 2005 at 8:07 am
Sorry. I did have to change the join to a left outer join.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply