February 8, 2008 at 7:40 am
I have the following recordset being returned and I need to narrow it down even further:
ConsultantID Level Title PeriodEndDate
0002617 02 Consultant 2001-08-31
0002617 04 Team Leader 2002-03-31
0002617 05 Team Manager 2002-06-30
0002617 06 Senior Team Manager2002-09-30
0002617 03 Senior Consultant2007-08-31
0002617 04 Team Leader 2007-09-30
0002617 05 Team Manager 2008-01-31
What I am after is a history or repromotions, so in this case this consultant has repromoted twice so the new recordset would look like this:
Team Leader 2007-09-30
Team Manager 2008-01-31
Any ideas?
Thanks,
Art
Database Analyst
Tastefully Simple, Inc.
alorenzini@tastefullysimple.com
" Some days, it's not even worth chewing through the restraints!
February 8, 2008 at 7:53 am
You could use the row_number() function, so in this case you would number the promotions for a particular person to a particular level, and ignore the first promotion to this level by requiring this number to be greater than one (i.e. the second, third, ... promotion)
SELECT ConsultantID
, Level
, Title
, PeriodEndDate
FROM ( SELECT row_number() OVER ( PARTITION BY Level ORDER BY PeriodEndDate ) AS Nr
, ConsultantID
, Level
, Title
, PeriodEndDate
FROM ... the original query
) AS x
WHERE x.Nr > 1
Regards,
Andras
February 8, 2008 at 8:11 am
I adapted your query and it looks like this:
SELECT ConsultantID
, Level
, Title
, PeriodEndDate
FROM
( SELECT
row_number() OVER ( PARTITION BY Level ORDER BY PeriodEndDate ) AS Nr
, ConsultantID
, Level
, Title
, PeriodEndDate
FROM (Select ConsultantID,
AchieveLevel,
AchieveTitle,
PeriodEndDate from #Temp
WHERE PaidLevel <> AchieveLevel ) AS x
WHERE x.Nr > 1
But I get the following syntax error:
Msg 102, Level 15, State 1, Line 17
Incorrect syntax near '1'.
Thanks,
Art
Database Analyst
Tastefully Simple, Inc.
alorenzini@tastefullysimple.com
" Some days, it's not even worth chewing through the restraints!
February 8, 2008 at 8:19 am
alorenzini (2/8/2008)
I adapted your query and it looks like this:SELECT ConsultantID
, Level
, Title
, PeriodEndDate
FROM
( SELECT
row_number() OVER ( PARTITION BY Level ORDER BY PeriodEndDate ) AS Nr
, ConsultantID
, Level
, Title
, PeriodEndDate
FROM (Select ConsultantID,
AchieveLevel,
AchieveTitle,
PeriodEndDate from #Temp
WHERE PaidLevel <> AchieveLevel ) AS x
WHERE x.Nr > 1
But I get the following syntax error:
Msg 102, Level 15, State 1, Line 17
Incorrect syntax near '1'.
You seem to have missed a closing parantheses:
SELECT ConsultantID
, Level
, Title
, PeriodEndDate
FROM ( SELECT row_number() OVER ( PARTITION BY Level ORDER BY PeriodEndDate ) AS Nr
, ConsultantID
, Level
, Title
, PeriodEndDate
FROM ( SELECT ConsultantID
, AchieveLevel
, AchieveTitle
, PeriodEndDate
FROM #Temp
WHERE PaidLevel <> AchieveLevel
)
) AS x
WHERE x.Nr > 1
Also, in this query (unlike in your original one), the ConsultantID could be different. You may want to add this column to the partition by column in the order clause.
Regards,
Andras
February 8, 2008 at 8:32 am
February 8, 2008 at 8:35 am
SELECT ConsultantID
, Level
, Title
, PeriodEndDate
FROM
( SELECT row_number() OVER ( PARTITION BY AchieveLevel ORDER BY PeriodEndDate ) AS Nr
, ConsultantID
, AchieveLevel
, AchieveTitle
, PeriodEndDate
FROM ( SELECT ConsultantID
, AchieveLevel
, AchieveTitle
, PeriodEndDate
FROM #Temp
WHERE PaidLevel <> AchieveLevel)
) AS x
WHERE x.Nr > 1
I am now getting the following error, does it have to do with AS x in the WHere clause?
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near ')'.
Thanks,
Art
Database Analyst
Tastefully Simple, Inc.
alorenzini@tastefullysimple.com
" Some days, it's not even worth chewing through the restraints!
February 8, 2008 at 8:51 am
SELECT DISTINCT b.ConsultantID,
b.AchieveLevel,
b.AchieveTitle,
b.PeriodEndDate
FROM #Temp a
INNER JOIN #Temp b ON a.ConsultantID = b.ConsultantID
AND a.PaidLevel =b.AchieveLevel AND a.PeriodEndDate < b.PeriodEndDate
ORDER BY b.PeriodEndDate
This returns the entire recordset not the two records I am looking for..
Thanks,
Art
Database Analyst
Tastefully Simple, Inc.
alorenzini@tastefullysimple.com
" Some days, it's not even worth chewing through the restraints!
February 8, 2008 at 9:05 am
You have something wrong, take a look my test data:
DECLARE @C TABLE(
ConsultantID CHAR(7),
Level CHAR(2),
Title CHAR(25),
PeriodEndDate DATETIME
)
INSERT INTO @C
SELECT '0002617', '02', 'Consultant', '2001-08-31' UNION ALL
SELECT '0002617', '04', 'Team Leader', '2002-03-31' UNION ALL
SELECT '0002617', '05', 'Team Manager', '2002-06-30' UNION ALL
SELECT '0002617', '06', 'Senior Team Manager', '2002-09-30' UNION ALL
SELECT '0002617', '03', 'Senior Consultant', '2007-08-31' UNION ALL
SELECT '0002617', '04', 'Team Leader', '2007-09-30' UNION ALL
SELECT '0002617', '05', 'Team Manager', '2008-01-31'
--Team Leader 2007-09-30
--Team Manager 2008-01-31
SELECT DISTINCT b.ConsultantID, b.Level, b.Title, b.PeriodEndDate
FROM @C a INNER JOIN @C b ON a.ConsultantID = b.ConsultantID
AND a.Level = b.Level AND a.PeriodEndDate < b.PeriodEndDate
ORDER BY b.PeriodEndDate
February 8, 2008 at 9:09 am
SELECT DISTINCT b.ConsultantID,
b.AchieveLevel,
b.AchieveTitle,
b.PeriodEndDate
FROM #Temp a
INNER JOIN #Temp b ON a.ConsultantID = b.ConsultantID
AND a.PaidLevel =b.AchieveLevel AND a.PeriodEndDate < b.PeriodEndDate
ORDER BY b.PeriodEndDate
This returns the entire recordset not the two records I am looking for..
The query works off the ddl you posted that there is only one "Level" column. To get this to work you have to set a.level = b.level.
February 8, 2008 at 10:19 am
This almost works but not quite, here is another record set:
COnsultantID Level Title PeriodEndDate
0002354 01 NAC Consultant02/28/2001
0002354 02 Consultant 03/31/2001
0002354 02 Consultant 04/30/2001
0002354 02 Consultant 05/31/2001
0002354 02 Consultant 06/30/2001
0002354 02 Consultant 07/31/2001
0002354 02 Consultant 08/31/2001
0002354 02 Consultant 09/30/2001
0002354 02 Consultant 10/31/2001
0002354 02 Consultant 11/30/2001
0002354 04 Team Leader 12/31/2001
0002354 04 Team Leader 01/31/2002
0002354 04 Team Leader 02/28/2002
0002354 04 Team Leader 03/31/2002
0002354 04 Team Leader 04/30/2002
0002354 04 Team Leader 05/31/2002
0002354 04 Team Leader 06/30/2002
0002354 04 Team Leader 07/31/2002
0002354 04 Team Leader 08/31/2002
0002354 04 Team Leader 09/30/2002
0002354 04 Team Leader 10/31/2002
0002354 04 Team Leader 11/30/2002
0002354 05 Team Manager 12/31/2002
0002354 05 Team Manager 01/31/2003
0002354 05 Team Manager 02/28/2003
0002354 05 Team Manager 03/31/2003
0002354 05 Team Manager 04/30/2003
0002354 04 Team Leader 05/31/2003
0002354 04 Team Leader 06/30/2003
0002354 04 Team Leader 07/31/2003
0002354 04 Team Leader 08/31/2003
0002354 05 Team Manager 09/30/2003
0002354 05 Team Manager 10/31/2003
0002354 05 Team Manager 11/30/2003
0002354 05 Team Manager 12/31/2003
0002354 05 Team Manager 01/31/2004
0002354 04 Team Leader 02/29/2004
0002354 04 Team Leader 03/31/2004
0002354 04 Team Leader 04/30/2004
0002354 04 Team Leader 05/31/2004
0002354 04 Team Leader 06/30/2004
0002354 04 Team Leader 07/31/2004
0002354 04 Team Leader 08/31/2004
0002354 04 Team Leader 09/30/2004
0002354 05 Team Manager 10/31/2004
0002354 05 Team Manager 11/30/2004
0002354 05 Team Manager 12/31/2004
0002354 05 Team Manager 01/31/2005
0002354 05 Team Manager 02/28/2005
0002354 05 Team Manager 03/31/2005
0002354 03 Senior Consultant04/30/2005
0002354 03 Senior Consultant05/31/2005
0002354 03 Senior Consultant06/30/2005
0002354 03 Senior Consultant07/31/2005
0002354 03 Senior Consultant08/31/2005
0002354 03 Senior Consultant09/30/2005
0002354 03 Senior Consultant10/31/2005
0002354 03 Senior Consultant11/30/2005
0002354 03 Senior Consultant12/31/2005
0002354 04 Team Leader 01/31/2006
0002354 04 Team Leader 02/28/2006
0002354 04 Team Leader 03/31/2006
0002354 03 Senior Consultant04/30/2006
0002354 03 Senior Consultant05/31/2006
0002354 03 Senior Consultant06/30/2006
0002354 03 Senior Consultant07/31/2006
0002354 03 Senior Consultant08/31/2006
0002354 03 Senior Consultant09/30/2006
0002354 03 Senior Consultant10/31/2006
0002354 03 Senior Consultant11/30/2006
0002354 03 Senior Consultant12/31/2006
0002354 03 Senior Consultant01/31/2007
0002354 03 Senior Consultant02/28/2007
0002354 03 Senior Consultant03/31/2007
0002354 03 Senior Consultant04/30/2007
0002354 03 Senior Consultant05/31/2007
0002354 03 Senior Consultant06/30/2007
0002354 03 Senior Consultant07/31/2007
0002354 03 Senior Consultant08/31/2007
0002354 03 Senior Consultant09/30/2007
0002354 03 Senior Consultant10/31/2007
0002354 03 Senior Consultant11/30/2007
0002354 03 Senior Consultant12/31/2007
0002354 03 Senior Consultant01/31/2008
In this case, the recordset should return
0002354 05 Team Manager
0002354 04 Team Leader
0002354 03 Senior Consultant
Because these were the repromotes. Instead it is just returning two records for Team Leader. THis is the code as it sits now:
SELECT * INTO #Temp from volume a
where exists (select * from volume b
where Repflag='D' and
b.consultantid=a.consultantid
--and b.periodEndDate<a.periodenddate
and a.AchieveLevel<>b.achieveLevel AND ConsultantID = '0002354')
Select ConsultantID, AchieveLevel, AchieveTitle, PeriodEndDate into #Temp2 from #Temp a
WHERE PaidLevel <> AchieveLevel
SELECT DISTINCT b.ConsultantID, b.AchieveLevel, b.AchieveTitle, b.PeriodEndDate
FROM #Temp2 a INNER JOIN #Temp2 b ON a.ConsultantID = b.ConsultantID
AND a.AchieveLevel = b.AchieveLevel AND a.PeriodEndDate < b.PeriodEndDate
ORDER BY b.PeriodEndDate
Thanks,
Art
Database Analyst
Tastefully Simple, Inc.
alorenzini@tastefullysimple.com
" Some days, it's not even worth chewing through the restraints!
February 8, 2008 at 10:56 am
THis might help give you an idea of the records that needed to be pulled out (see attachment) the high lighted rows are the rows that need to be returned:
Thanks,
Art
Database Analyst
Tastefully Simple, Inc.
alorenzini@tastefullysimple.com
" Some days, it's not even worth chewing through the restraints!
February 8, 2008 at 12:56 pm
Alorenzini,
I tried to avoid using a type of cursor for this query but I could not avoid it. Maybe some of the others guys will have some better methods. This query will return the data you are looking for.
DECLARE @C TABLE(
Row_ID INT IDENTITY(1,1),
ConsultantID CHAR(7),
Level CHAR(2),
Title CHAR(25),
PeriodEndDate DATETIME
)
INSERT INTO @C
SELECT '0002354', '01' ,'NAC Consultant', '02/28/2001' UNION ALL
SELECT '0002354', '02' ,'Consultant', '03/31/2001' UNION ALL
SELECT '0002354', '02' , 'Consultant', '04/30/2001' UNION ALL
SELECT '0002354', '02' , 'Consultant', '05/31/2001' UNION ALL
SELECT '0002354', '02' , 'Consultant', '06/30/2001' UNION ALL
SELECT '0002354', '02' , 'Consultant', '07/31/2001' UNION ALL
SELECT '0002354', '02' , 'Consultant', '08/31/2001' UNION ALL
SELECT '0002354', '02' , 'Consultant', '09/30/2001' UNION ALL
SELECT '0002354', '02' , 'Consultant', '10/31/2001' UNION ALL
SELECT '0002354', '02' , 'Consultant', '11/30/2001' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '12/31/2001' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '01/31/2002' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '02/28/2002' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '03/31/2002' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '04/30/2002' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '05/31/2002' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '06/30/2002' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '07/31/2002' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '08/31/2002' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '09/30/2002' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '10/31/2002' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '11/30/2002' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '12/31/2002' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '01/31/2003' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '02/28/2003' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '03/31/2003' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '04/30/2003' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '05/31/2003' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '06/30/2003' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '07/31/2003' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '08/31/2003' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '09/30/2003' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '10/31/2003' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '11/30/2003' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '12/31/2003' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '01/31/2004' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '02/29/2004' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '03/31/2004' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '04/30/2004' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '05/31/2004' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '06/30/2004' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '07/31/2004' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '08/31/2004' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '09/30/2004' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '10/31/2004' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '11/30/2004' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '12/31/2004' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '01/31/2005' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '02/28/2005' UNION ALL
SELECT '0002354', '05' , 'Team Manager', '03/31/2005' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '04/30/2005' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '05/31/2005' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '06/30/2005' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '07/31/2005' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '08/31/2005' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '09/30/2005' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '10/31/2005' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '11/30/2005' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '12/31/2005' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '01/31/2006' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '02/28/2006' UNION ALL
SELECT '0002354', '04' , 'Team Leader', '03/31/2006' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '04/30/2006' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '05/31/2006' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '06/30/2006' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '07/31/2006' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '08/31/2006' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '09/30/2006' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '10/31/2006' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '11/30/2006' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '12/31/2006' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '01/31/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '02/28/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '03/31/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '04/30/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '05/31/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '06/30/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '07/31/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '08/31/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '09/30/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '10/31/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '11/30/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '12/31/2007' UNION ALL
SELECT '0002354', '03' , 'Senior Consultant', '01/31/2008'
DECLARE @counter INT,
@prev CHAR(2),
@NbrRows INT
SET @counter = 1
SET @prev = NULL
SET @NbrRows = (SELECT COUNT(*) FROM @C)
DECLARE @r TABLE(
ConsultantID CHAR(7),
Level CHAR(2),
Title CHAR(25),
PeriodEndDate DATETIME
)
WHILE @counter <= @NbrRows
BEGIN
INSERT INTO @r
SELECT a.ConsultantID, a.Level, a.Title,a.PeriodEndDate
FROM @C a
WHERE a.Row_Id = @counter AND a.Level > @prev AND a.Level IN
(SELECT b.Level
FROM @C b
WHERE b.Row_ID BETWEEN 1 AND @counter - 1)
SELECT @prev = a.Level
FROM @C a
WHERE a.Row_ID = @counter
SET @counter = @counter + 1
END
SELECT *
FROM @r
February 8, 2008 at 1:17 pm
Art - this is different from what we tried to tackle in December?
I'm talking about this thread:
http://www.sqlservercentral.com/Forums/Topic431378-338-2.aspx
Just curious. If you can get a full list of the "rules" - we can try to figure out what no longer works from the other one.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
February 8, 2008 at 5:53 pm
And, maybe learn how to post sample data to get better answers...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
--Jeff Moden
Change is inevitable... Change for the better is not.
February 9, 2008 at 3:11 am
Using Adam's data
WITH Promotions AS (
SELECT a.ConsultantID,
a.Level,
a.Title,
a.PeriodEndDate,
ROW_NUMBER() OVER(PARTITION BY a.ConsultantID, a.Level ORDER BY a.PeriodEndDate) as rn
FROM @C a
WHERE EXISTS (SELECT * FROM @C b
WHERE b.ConsultantID=a.ConsultantID
AND b.Level<a.Level
AND b.PeriodEndDate<a.PeriodEndDate
AND NOT EXISTS (SELECT * FROM @C c
WHERE c.ConsultantID=a.ConsultantID
AND c.PeriodEndDate>b.PeriodEndDate
AND c.PeriodEndDate<a.PeriodEndDate))
)
SELECT ConsultantID,
Level,
Title,
PeriodEndDate
FROM Promotions
WHERE rn>1
ORDER BY PeriodEndDate
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537Viewing 15 posts - 1 through 15 (of 124 total)
You must be logged in to reply to this topic. Login to reply