以下是我的表概述(Task,TaskSequence,OverallFileProgress和FileProgressStage)
任务:
TaskId TaskName
1 File Opened
2 File Assigned
3 File Disposed
TaskSequence :
TaskSequenceId SequenceNumber TaskId
1 33 3
2 11 1
3 22 2
OverallFileProgress :
OverallFileProgressId WorkId
1 100
2 101
FileProgressStage :
FileProgressStageId OverallFileProgressId TaskId IsCompleted
1 1 1 1
2 1 2 1
3 1 3 0
6 2 1 1
7 2 2 0
8 2 3 0
FileProgressStage
表用于跟踪每个阶段的文件进度,并将该阶段标记为完成。这类似于Office文件的工作流跟踪。现在我想展示WorkId的当前阶段。根据上述数据,我希望得到下一个未完成的工作当前阶段:
WorkId TaskId TaskName
100 3 File Disposed
101 2 File Assigned
此处,下一阶段的任务由TaskSequence
表格顺序由SequenceNumber
决定。因此,每个WorkId
将按照TaskSequence
表中定义的顺序顺序流动。这意味着WorkId
将从FileOpened =>流出FileAssigned => FileDisposed
答案 0 :(得分:1)
假设您正在使用tSQL,这应该可以工作:
创建数据集:
CREATE TABLE #Task
(
TaskId INT, TaskName VARCHAR(50)
);
INSERT INTO #Task
VALUES
(1, 'File Opened'
),
(2, 'File Assigned'
),
(3, 'File Disposed'
);
CREATE TABLE #TaskSequence
(
TaskSequenceId INT, SequenceNumber INT, TaskId INT
);
INSERT INTO #TaskSequence
VALUES
(1, 33, 3
),
(2, 11, 1
),
(3, 22, 2
);
CREATE TABLE #OverallFileProgress
(
OverallFileProgressId INT, WorkId INT
);
INSERT INTO #OverallFileProgress
VALUES
(1, 100
),
(2, 101
);
CREATE TABLE #FileProgressStage
(
FileProgressStageId INT, OverallFileProgressId INT, TaskId INT, IsCompleted INT
);
INSERT INTO #FileProgressStage
VALUES
(1, 1, 1, 1
),
(2, 1, 2, 1
),
(3, 1, 3, 0
),
(6, 2, 1, 1
),
(7, 2, 2, 0
),
(8, 2, 3, 0
);
解决方案:
;WITH CTE AS (
SELECT C.WorkId , A.TaskId,B.TaskName , ROW_NUMBER() OVER (PARTITION BY C.WorkId ORDER BY S.SequenceNumber ) AS rnk
FROM #FileProgressStage AS A
JOIN #Task AS B ON A.TaskId = B.TaskId
JOIN #OverallFileProgress AS C ON A.OverallFileProgressId=C.OverallFileProgressId
JOIN TaskSequence AS S on B.TaskId=S.TaskId
WHERE A.IsCompleted <> 1 )
SELECT cte.WorkId,cte.TaskId,cte.TaskName
FROM CTE
WHERE rnk = 1
结果: