Hi,
I am having trouble figuring out how to retrieve the data I need from a SQL Server 2000 database.
My table looks like the following:
RecId Int,
EmpID Int,
StartDate datetime,
Description varchar(30)
There may be multiple records with the same EmpId. What I want to do is retrieve the top three records per empid, ordered by Startdate, in a single row. For example, if my table contains the following:
100, 1001, '2001-01-01', 'Description 1a'
101, 1001, '2001-05-01', 'Description 1b'
102, 1001, '2001-07-01', 'Description 1c'
103, 1001, '2000-06-01', 'Description 1d'
104, 1002, '2001-09-01', 'Description 2a'
105, 1002, '2001-03-01', 'Description 2b'
106, 1002, '2001-04-01', 'Description 2c'
How can I get the following as a result of my query:
1001, '2001-07-01', 'Description 1c', '2001-05-01', 'Description 1b', '2001-01-01', 'Description 1a'
1002, '2001-09-01', 'Description 2a', '2001-04-01', 'Description 2c', '2001-03-01', 'Description 2b'
Now I have tried the following, which works but takes far too much time:
Select EmpId,
(Select Top 1 StartDate From myTable Where EmpID = Main.EmpId Order by StartDate Desc) As Date1,
(Select Top 1 Description From myTable Where EmpID = Main.EmpId Order by StartDate Desc) As Desc1,
(Select Top 1 StartDate From myTable TB1 where TB1.EmpID = Main.EmpId
And TB1.RecId Not In (Select Top 1 RecId From myTable TB2 Where TB2.EmpID = Main.EmpId Order by TB2.StartDate Desc)
Order by StartDate Desc) As Date2,
(Select Top 1 Description From myTable TB3 Where TB3.EmpId = Main.EmpID
And TB3.RecId Not In (select Top 1 RecId From myTable TB4 Where TB4.EmpId = Main.EmpID Order by TB4.StartDate Desc)
Order by StartDate Desc) as Desc2,
(Select Top 1 StartDate From myTable TB5 Where TB5.EmpId = Main.EmpID
And TB5.RecId Not In (Select Top 2 RecId From myTable TB1 where TB1.EmpID = Main.EmpId
Order by StartDate Desc)
Order by StartDate Desc) As Date3,
(Select Top 1 Description From MyTable TB7 Where TB7.EmpId = Main.EmpId
And TB7.RecId Not In (Select Top 2 RecId From myTable TB1 where TB1.EmpID = Main.EmpId
Order by StartDate Desc)
Order by StartDate Desc) As Desc3
From myTable Main
Group by EmpId
Is there another approach that would be more efficient?
I am having trouble figuring out how to retrieve the data I need from a SQL Server 2000 database.
My table looks like the following:
RecId Int,
EmpID Int,
StartDate datetime,
Description varchar(30)
There may be multiple records with the same EmpId. What I want to do is retrieve the top three records per empid, ordered by Startdate, in a single row. For example, if my table contains the following:
100, 1001, '2001-01-01', 'Description 1a'
101, 1001, '2001-05-01', 'Description 1b'
102, 1001, '2001-07-01', 'Description 1c'
103, 1001, '2000-06-01', 'Description 1d'
104, 1002, '2001-09-01', 'Description 2a'
105, 1002, '2001-03-01', 'Description 2b'
106, 1002, '2001-04-01', 'Description 2c'
How can I get the following as a result of my query:
1001, '2001-07-01', 'Description 1c', '2001-05-01', 'Description 1b', '2001-01-01', 'Description 1a'
1002, '2001-09-01', 'Description 2a', '2001-04-01', 'Description 2c', '2001-03-01', 'Description 2b'
Now I have tried the following, which works but takes far too much time:
Select EmpId,
(Select Top 1 StartDate From myTable Where EmpID = Main.EmpId Order by StartDate Desc) As Date1,
(Select Top 1 Description From myTable Where EmpID = Main.EmpId Order by StartDate Desc) As Desc1,
(Select Top 1 StartDate From myTable TB1 where TB1.EmpID = Main.EmpId
And TB1.RecId Not In (Select Top 1 RecId From myTable TB2 Where TB2.EmpID = Main.EmpId Order by TB2.StartDate Desc)
Order by StartDate Desc) As Date2,
(Select Top 1 Description From myTable TB3 Where TB3.EmpId = Main.EmpID
And TB3.RecId Not In (select Top 1 RecId From myTable TB4 Where TB4.EmpId = Main.EmpID Order by TB4.StartDate Desc)
Order by StartDate Desc) as Desc2,
(Select Top 1 StartDate From myTable TB5 Where TB5.EmpId = Main.EmpID
And TB5.RecId Not In (Select Top 2 RecId From myTable TB1 where TB1.EmpID = Main.EmpId
Order by StartDate Desc)
Order by StartDate Desc) As Date3,
(Select Top 1 Description From MyTable TB7 Where TB7.EmpId = Main.EmpId
And TB7.RecId Not In (Select Top 2 RecId From myTable TB1 where TB1.EmpID = Main.EmpId
Order by StartDate Desc)
Order by StartDate Desc) As Desc3
From myTable Main
Group by EmpId
Is there another approach that would be more efficient?