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
 General SQL Server Forums
 New to SQL Server Programming
 SQL Optimization

Author  Topic 

spendyala
Starting Member

15 Posts

Posted - 2011-02-16 : 12:44:06
Hi All,

I need your help in figuring out this issue. I am working on the logic to see the account status of an employee at regular intervals, lets say there is an employee A and his account status are active, inactive, referral, interview and so on. i have to write a query to get his status at 30, 60, 90, 120, 150...270 days from the date of his interview. all this status is stored in one table. To get the required result, i am using dateadded and hitting the table that many times in the select statement like
select employee,
(select employeestatus where date < interviewdate + 30),
(select employeestatus where date < interviewdate + 60) and so on.. The employee table is huge and is taking an hour or so to get the result...please let me know if there is a better way...


Adding the Employee TAble

CREATE TABLE #Employee (EmployeeID INT, Status VARCHAR(20), STatusDAte DATETIME)
INSERT #Employee
SELECT 1,'Interview','2010-01-01'
UNION ALL
SELECT 1,'Applicant','2010-02-15'
UNION ALL
SELECT 1,'Inactive','2010-04-01'
UNION ALL
SELECT 1,'PendingAppr','2010-06-01'
UNION ALL
SELECT 1,'FInterview','2010-06-15'
UNION ALL
SELECT 1,'Active','2010-06-30'
UNION ALL
SELECT 2,'Interview','2010-01-01'
UNION ALL
SELECT 2,'Applicant','2010-02-15'
UNION ALL
SELECT 2,'Inactive','2010-04-01'
UNION ALL
SELECT 2,'PendingAppr','2010-06-01'
UNION ALL
SELECT 2,'FInterview','2010-06-15'
UNION ALL
SELECT 2,'Active','2010-06-30'




Thank you, Pendyala

RobertKaucher
Posting Yak Master

169 Posts

Posted - 2011-02-16 : 15:06:12
http://blog.sqlauthority.com/2007/06/08/sql-server-insert-multiple-records-using-one-insert-statement-use-of-union-all/

Look at the comments to the page above.

===
http://www.ElementalSQL.com/
Go to Top of Page
   

- Advertisement -