I have a table with data as below:
Id stage date Emp_id3
===== ===== ===== =====
DEP1 new 12thmarch 33
DEP1 approval 13thmarch 22
DEP1 reject 14thmarch 77
DEP1 approval 15thmarch 66
DEP1 reject 16thmarch 65
DEP1 approval 17thmarch 87
DEP1 complete 18thmarch 99
I want to find that Emp_id3 who has moved the staging data from reject to approval stage. In this case it will be 87 as it is the latest approval. This is the audit table that has stage wise data for its master table which has current stage data. Can some one please help me with the sybase query for the same? Any help will be much appreciated.
The problem you described hits cumber stone Sybase ase issue, no rank function. Bellow I have created a sample solution that involves using of temporary tables. Additional assumption is that date should be in format that ensures uniqueness ( DATETIME should be enough).
CREATE TABLE #temp
(
id VARCHAR(20),
stage VARCHAR(20),
DATE INT,
Emp_id3 INT)
INSERT INTO #temp
select "DEP1", "new", 12,33
union select "DEP1", "approval",13,22
union select "DEP1", "reject", 14,77
union select "DEP1", "approval",15,66
union select "DEP1", "reject", 16,65
union select "DEP1", "approval",17,87
union select "DEP1", "complete",18,99
SELECT *, rank = identity(1) into #temp_rk FROM #temp ORDER BY DATE
SELECT changer.Emp_id3 FROM
#temp_rk changer,#temp_rk originator
WHERE
changer.stage="approval"
AND originator.stage ="reject"
AND changer.rank -1 = originator.rank
HAVING changer.rank = max(changer.rank)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句