Select 来自 sybase 的特定暂存记录 table

Select a specific staging record from sybase table

我有一个 table 数据如下:

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,因为它是最新的批准。 这是审计 table,它的主 table 有阶段性数据,它有当前阶段数据。有人可以帮我做同样的 sybase 查询吗?任何帮助将不胜感激。

你描述的问题是打cumber stone Sybase ase的问题,没有rank函数。 下面我创建了一个涉及使用临时表的示例解决方案。额外的假设是日期的格式应该确保唯一性( 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)