I have three tables that contain data relating to a Job. One table is a master table and the other two are transactional and are related to the master.
CREATE TABLE Job
(JobNo varchar(10))
CREATE TABLE Trans1
(JobNo varchar(10), TrxDate datetime, TrxStatus int)
CREATE TABLE Trans2
(JobNo varchar(10), TrxDate datetime, TrxStatus int)
The Job table will always have one row per job. Trans1 and Trans2 will have none, one or many rows per job.
I need to be able to write a query that will return, for a given job, what the TrxStatus column’s value was on a given date.
Let’s say Trans1 has
Job TrxDate TrxStatus
AB123 2/1/2012 10
AB123 3/1/2012 20
AB123 3/31/2021 20
Let’s say Trans2 has
Job TrxDate TrxStatus
AB123 3/15/2012 10
I need to be able to create a function (or have a solution) where I can query:
SELECT JobNo, GetStatusAt(JobNo, '3/1/2012') FROM Job
and have it return 20.
SELECT JobNo, GetStatusAt(JobNo, '3/17/2012') FROM Job
and have it return 10.
SELECT JobNo, GetStatusAt(JobNo, '4/1/2012') FROM Job
and have it return 20.
1 Answer