Subject | How do I formulate this... |
---|---|
Author | DaveR |
Post date | 2002-03-19T00:45:51Z |
I can't get my head round this one...
I have a table define as
CREATE TABLE CONTACTHISTORY
(
CONTACTID D_ID , integer not null
CONTACTTYPEID D_ID ,
CONTACTDATE D_DATE , Date not null
USERID D_ID ,
NOTES D_NOTES , VarChar(80)
constraint PK_CONTACTHISTORY PRimary Key (ContactId, ContactType,
ContactDate),
constraint FK_CH_CONTACT Foreign Key (ContactId) references
Contacts(Id),
constraint FK_CH_CONTACTTYPE Foreign Key (ContactTypeId) references
ContactTypes(Id),
constraint FK_CH_USER Foreign Key (UserId) references Users(Id)
)
and I want to select the most recent record of each contacttype.
One way to do it would be to write a procedure using a cursor through
the CONTACTTYPES table doing a select on the CONTACTHISTORY table for
each record on the CONTACTTYPES table.
Is there a way this can be done in single query? I think there is, but
cannot quite formulate it.
Any pointers would be appreciated.
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.338 / Virus Database: 189 - Release Date: 14/03/2002
I have a table define as
CREATE TABLE CONTACTHISTORY
(
CONTACTID D_ID , integer not null
CONTACTTYPEID D_ID ,
CONTACTDATE D_DATE , Date not null
USERID D_ID ,
NOTES D_NOTES , VarChar(80)
constraint PK_CONTACTHISTORY PRimary Key (ContactId, ContactType,
ContactDate),
constraint FK_CH_CONTACT Foreign Key (ContactId) references
Contacts(Id),
constraint FK_CH_CONTACTTYPE Foreign Key (ContactTypeId) references
ContactTypes(Id),
constraint FK_CH_USER Foreign Key (UserId) references Users(Id)
)
and I want to select the most recent record of each contacttype.
One way to do it would be to write a procedure using a cursor through
the CONTACTTYPES table doing a select on the CONTACTHISTORY table for
each record on the CONTACTTYPES table.
Is there a way this can be done in single query? I think there is, but
cannot quite formulate it.
Any pointers would be appreciated.
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.338 / Virus Database: 189 - Release Date: 14/03/2002