0

考虑下表:

应用

+----+-------------+---------+----------+
| id | previous_id | next_id | status   |
+----+-------------+---------+----------+
| 1  |   NULL      |    3    | Archived |
+----+-------------+---------+----------+
| 3  |     1       |    4    | Archived |
+----+-------------+---------+----------+
| 4  |     3       |   NULL  | Approved |
+----+-------------+---------+----------+

注释

+-----+--------+----------------+
| id  | app_id |   comment      |
+-----+--------+----------------+
|  1  |   1    |  Testing       |
+-----+--------+----------------+
|  2  |   1    |  Still testing |
+-----+--------+----------------+
|  3  |   4    |  No longer     |
+-----+--------+----------------+

应用程序表包含应用程序的修订。注释表包含在应用程序的每次迭代中留下的注释。它们通过以下方式联系在一起:

Applications.ID = Comments.APP_ID

有一个面包屑会告诉您应用程序是否有任何先前的修订,以及您是否正在处理当前修订(当前行将始终具有 NULL 作为 next_id 值。

我想建立两件事。

  1. 应用程序有多少次修订的计数。它可能是零修订版,也可能是 16+。
  2. 任何给定当前应用程序(如 ID = 4)和所有以前的应用程序的评论列表,按照最新评论到最旧评论的顺序排列。
4

2 回答 2

1

使用递归 CTE,您可以为任何给定的应用程序构建修订列表。以下查询将为您提供修订/评论列表,按修订(最新优先)和评论 id(最新优先)排序,由基本应用程序分隔。每一行都有一个额外的计数列revision_count重复,所以这一切都在一个查询中完成。请注意,修订计数是从 0 开始的;也就是说,count是base的修改次数,不包括base。我相信这就是你想要的。结果列revision是序列中从 1 开始的修订号(1 是最旧的基础修订)。

您当然可以根据需要进行推断或调整。

;WITH Base AS
(
    SELECT id, next_id, status, 1 AS revision
    FROM Applications
    WHERE previous_id IS NULL
), Revisions AS
(
   SELECT id, next_id, status, revision, id AS BASEID
   FROM Base
   UNION ALL
   SELECT a.id, a.next_id, a.status, r.revision + 1 AS revision, r.BASEID
   FROM Applications a
   INNER JOIN Revisions r ON a.id = r.next_id
), RevisionCounts AS
(
   SELECT COUNT(1) - 1 AS revision_count, BASEID
   FROM Revisions
   GROUP BY BASEID
)
SELECT r.BASEID, r.revision, r.id, r.status, c.comment, rc.revision_count
FROM Revisions r
LEFT OUTER JOIN Comments c ON c.app_id = r.id
INNER JOIN RevisionCounts rc ON r.BASEID = rc.BASEID
ORDER BY r.BASEID, r.revision DESC, c.id DESC 

这是一个伴随它的小提琴,展示了查询的实际效果。返回的数据是:

BASEID REVISION ID STATUS COMMENT REVISION_COUNT
1 3 4 不再批准 2
1 2 3 已存档(空) 2
1 1 1 存档 仍在测试 2
1 1 1 存档测试 2
于 2013-07-24T18:37:26.100 回答
0
select 
ID, count(previous_id) 
from 
applications 
where 
previous_id is not null group by id ---record is not new
UNION
Select 
ID,0 
from 
applications 
where 
previous_id is null--when the record is new and does not have any revisions

select 
a.id, comments 
from 
applications a join comments c on a.id=c.id
order by 
C.id 
于 2013-07-24T18:36:39.017 回答