Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Finding first date within fields

Author  Topic 

mrswwilson
Starting Member

2 Posts

Posted - 2005-06-27 : 11:05:15
Hi all, I am an SQL newbie, so hopefully someone can point me in the right direction:-
I am using SQL Server8, and have a dbo with the following variables:
PatientID,PrimaryOpDate,SecOpDate1,SecOpDate2,SecOpDate3,PrimaryOpCode,SecOpCode1,SecOpCode2,SecOpCode3.
I want to be able to find the first date from the 4 date fields, and then return the associated code, where:
PrimaryOpDate is associated with PrimaryOpCode,
SecOpDate1 is associate with SecOpCode1 etc.
Many thanks for any help someone can give, I just need to know the start of the syntax I should use within my query.
Thanks
Scott.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-06-27 : 11:54:58
Is it possible to re-organize your table(s) so that you have a row for each OpDate/OpCode?
ie:


create table opCodeType
(opCodeTypeID tinyint
,opCodeTypeDesc varchar(50))
go
create table myTable
(PatientID int
,Opdate datetime
,OpCode int
,OpCodeTypeID tinyint references opCodeType(opCodeTypeID))

go

insert opCodeType values (1, 'Primary')
insert opCodeType values (2, 'Secondary')
go

--then your query could be something like:

select patientID
,opDate
,opCode
,opCodeTypeDesc
from myTable a
join (--derived table to get first date for each Patient
select patientID
,min(opDate) opDate
from myTable
group by patientID
) b
on a.patientID = b.patientID
and a.opDate = b.opDate
join opCodeType c
on a.opCodeTypeID = c.opCodeTypeDesc


Be One with the Optimizer
TG
Go to Top of Page

mrswwilson
Starting Member

2 Posts

Posted - 2005-06-27 : 12:04:20
TG, thanks for your reply. I am really a VBA novice, but i will give what you are suggesting a go, and will submit a new post if it all goes wrong!
thanks again
Scott
Go to Top of Page
   

- Advertisement -