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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 I Need Help Selecting Correct Status

Author  Topic 

eastern40
Starting Member

3 Posts

Posted - 2010-03-24 : 09:29:39
I looked around but I could not find a problem like mine in the forums. If anyone can redirect me if it has been answered that would be great if not, I am open to any suggestion to my issue.

Two tables:

T1 -> Dateassigned Status Name


T2 -> Dateoccured Name ThingOccured


Table one is a status table for a person. The person changes statuses and there is a date recorded when the status changes. But it only records when the new status begins, so this could be 1 time or 50.

The second table records when a person does something, this could be 1 or 50 also. The only field to join on is Name (atleast it is consistent)

Problem. I need to know what the persons status was at the time the thing occured. Not what their current staus is unless of course the thing occured after the max assigneddate.

Any resolution in stored proc or sql statement is possible for me. I can also create temp tables etc....I just cannot change the application that creates the data.

I'm stumped any help would be appreciated. Thank You

eastern40
Starting Member

3 Posts

Posted - 2010-03-24 : 10:54:39
This is what I have so far...My real world values are a bit more complicated but does anyone see anything wrong with this approach.

create table #T1 (datemoved datetime, status varchar(1), keys int)
create table #T2 (dateaccomplished datetime, keys int)

insert into #T1 (datemoved, status, keys) values ('1/1/2000','A',1)
insert into #T1 (datemoved, status, keys) values ('2/1/2000','B',1)
insert into #T1 (datemoved, status, keys) values ('3/1/2000','C',1)
insert into #T1 (datemoved, status, keys) values ('2/1/2000','F',2)
insert into #T1 (datemoved, status, keys) values ('3/1/2000','C',2)

insert into #T2 (dateaccomplished, keys) values ('1/2/2000',1)
insert into #T2 (dateaccomplished, keys) values ('3/4/2000',1)
insert into #T2 (dateaccomplished, keys) values ('2/5/2000',2)
insert into #T2 (dateaccomplished, keys) values ('3/10/2000',1)

create table #T3 (dateaccomplished datetime, status varchar(1), keys int)

--select * from #T1
--select * from #T2

declare @date1 datetime
declare @date2 datetime
declare @status varchar(1)
WHILE (select count(*) from #T1) > 0
BEGIN

set @date1 = (select top 1 datemoved from #T1 order by datemoved asc)

select @status = status from #T1 where datemoved = @date1

set @date2 = (select top 1 datemoved from #T1 where datemoved <> @date1 order by datemoved asc)

if @date2 is NULL
set @date2 = GETDATE()

insert into #T3 (dateaccomplished, status, keys)

select dateaccomplished, @status, keys from #T2 where dateaccomplished >= @date1 and dateaccomplished < @date2

delete from #T1 where datemoved = @date1

END


select * from #T3

--drop table #T1
--drop table #T2
--drop table #T3
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-24 : 11:42:58
[code]SELECT t2.Dateoccured,t2.Name,t2.ThingOccured,t1.Status AS StatusAtTimeOfOccurance
FROM T2 t2
CROSS APPLY (SELECT TOP 1 status
FROM T1
WHERE Name=t2.Name
AND Dateassigned<= t2.Dateoccured
ORDER BY Dateassigned DESC) t1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

eastern40
Starting Member

3 Posts

Posted - 2010-03-24 : 12:19:37
visakh16 = sql server mvp is true.

Thank You! That seems to work like a charm and alot more efficient then what I was trying. Thank you so much.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-24 : 12:27:54
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -