SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Earliest allocation query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Lijo Cheeran Joseph
Posting Yak Master

123 Posts

Posted - 10/30/2010 :  06:09:25  Show Profile  Reply with Quote
Hi Team,

I have the following schema. I want to list out all employee names and their first allocation (based on StartDate). The @ProjectAllocation has only the latest allocation. The previous allocation details are moved into @ProjectAllocHistory when (and ony when) a new allocation is done.

Note: Some employees will have records only in @ProjectAllocation; not in @ProjectAllocHistory.


Following is the expected result. Please help with the best possible query.

Lijo, '1/1/2008', 'COMPLETE'
Kiran, '1/1/2010', 'ACTIVE'


DECLARE @Employee TABLE (EmpID INT, EmpName VARCHAR(50))
INSERT INTO @Employee (EmpID , EmpName) VALUES (1,'Lijo')
INSERT INTO @Employee (EmpID , EmpName) VALUES (2,'Kiran')

DECLARE @ProjectAllocation TABLE (AllocID INT, ProjectID INT, EmpID INT, StartDate DATETIME, [Status] VARCHAR(10))
INSERT INTO @ProjectAllocation(AllocID , ProjectID ,EmpID, StartDate , [Status]) VALUES (3,17,1,'1/1/2010','ACTIVE')
INSERT INTO @ProjectAllocation(AllocID , ProjectID ,EmpID, StartDate , [Status]) VALUES (4,17,2,'1/1/2010','ACTIVE')

DECLARE @ProjectAllocHistory TABLE (AllocID INT, ProjectID INT, EmpID INT,StartDate DATETIME, [Status] VARCHAR(10))
INSERT INTO @ProjectAllocHistory(AllocID , ProjectID ,EmpID, StartDate , [Status]) VALUES (2,15,1,'1/1/2009','COMPLETE')
INSERT INTO @ProjectAllocHistory(AllocID , ProjectID ,EmpID, StartDate , [Status]) VALUES (1,13,1,'1/1/2008', 'COMPLETE')

SELECT *
FROM @Employee E
INNER JOIN @ProjectAllocation P
ON P.EmpID = E.EmpID
LEFT OUTER JOIN @ProjectAllocHistory H
ON H.EmpID = P.EmpID




Thanks
Lijo

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3377 Posts

Posted - 10/30/2010 :  08:58:31  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
;with cte as
(
SELECT E.EmpId, E.EmpName, P.StartDate, P.Status
FROM @Employee E
INNER JOIN @ProjectAllocation P
ON P.EmpID = E.EmpID
union all
SELECT E.EmpId, E.EmpName, h.StartDate, h.Status
FROM @Employee E
INNER JOIN @ProjectAllocHistory h
ON h.EmpID = E.EmpID
)
select EmpName, StartDate, Status
from
(
select *, seq = row_number() over (partition by EmpId order by StartDate)
from cte
) a
where seq = 1


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Sachin.Nand
Flowing Fount of Yak Knowledge

2937 Posts

Posted - 10/30/2010 :  12:15:43  Show Profile  Reply with Quote
OR


select E.EmpID,EmpName,Status from @Employee E cross apply
(
	select top 1 EmpID,[status] from
	(
		select * from @ProjectAllocation
		union all
		select * from @ProjectAllocHistory
		
	)T where T.EmpID=E.EmpId  order by StartDate 
	
)T1


PBUH

Go to Top of Page

Lijo Cheeran Joseph
Posting Yak Master

123 Posts

Posted - 10/30/2010 :  12:27:30  Show Profile  Reply with Quote
Thanks. This works.

However, is there any simplified version?
Go to Top of Page

Sachin.Nand
Flowing Fount of Yak Knowledge

2937 Posts

Posted - 10/30/2010 :  12:42:22  Show Profile  Reply with Quote
quote:
Originally posted by Lijo Cheeran Joseph

Thanks. This works.

However, is there any simplified version?



Can you please elaborate what do you mean by "simplified version"?

PBUH

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000