我有一张包含以下数据的表格:
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
我想找到将登台数据从拒绝阶段移到审批阶段的Emp_id3。在这种情况下,它将是87,因为它是最新的批准。 这是一个审计表,它具有包含当前阶段数据的主表的分阶段数据。有人可以帮我解决同样的sybase问题吗?任何帮助将不胜感激。
答案 0 :(得分:0)
你描述的问题击中了cumber stone Sybase ase问题,没有排名功能。 Bellow我创建了一个涉及使用临时表的示例解决方案。另外的假设是日期应采用确保唯一性的格式(DATETIME应该足够)。
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)