使用 CTE 获取每组的最后一行
Get last row of each group with CTE
我有这样的查询:
DECLARE @DeadDesigns AS TABLE(
LegacyKey INT
,DesignKey INT
,StatusKey INT
,DesignGroupId UNIQUEIDENTIFIER
)
INSERT INTO @DeadDesigns
SELECT [P].[LegacyKey],[D].DesignKey, [D].[StatusKey], [D].[DesignGroupId] FROM Project AS P
INNER JOIN DesignGroup AS DG ON P.ProjectKey = DG.ProjectKey
INNER JOIN Design AS D ON DG.DesignGroupId = D.DesignGroupId
WHERE [D].[StatusKey] = 67 --DEAD
;WITH CTE2 (LegacyKey, DesignKey, StatusKey, DesignGroupId, RN) AS (
SELECT LegacyKey, DesignKey, StatusKey, DesignGroupId,
ROW_NUMBER() OVER (PARTITION BY [LegacyKey], [DesignGroupId] ORDER BY [DesignGroupId]) AS RN
FROM @DeadDesigns
GROUP BY [DesignGroupId],[LegacyKey],DesignKey,StatusKey
)
SELECT * FROM CTE2
结构是:
一个LegacyKey
可以有多个DesignGroupId
,一个DesignGroupId
可以有多个DesignKey
Objective 是将每个 DesignGroupId
中的最后一个 DesignKey
从项目
中分离出来
错误结果:
+-----------+-----------+-----------+--------------------------------------+----+
| LegacyKey | DesignKey | StatusKEy | DesignGroupId | RN |
+-----------+-----------+-----------+--------------------------------------+----+
| 14002 | 2416 | 67 | 1A07C80E-E5E2-45F0-A5D2-419BAF3DC106 | 1 |
| 14002 | 2819 | 70 | 1A07C80E-E5E2-45F0-A5D2-419BAF3DC106 | 2 |
+-----------+-----------+-----------+--------------------------------------+----+
这是错误的,因为它是相同的 DesignGroupId,如果 DesignGroupId 不同,则结果是正确的。
详细愿望结果:
我想用 DesignGroupId
分隔它,所以如果有两个不同的 DesignGroupId
并且它们相同 LegacyKey
第二行的 RN
应该是 2.. 如果我们有 3 DesignGroupId
但第二行的相同 LegacyKey
RN
应该是 3 等等。最后我想在每个 LegacyKey
中的每个 DesignGroupId
中获取所有 last DesignKey
我做错了什么?
另一个例子
+-----------+-----------+-----------+--------------------------------------+----+
| LegacyKey | DesignKey | StatusKEy | DesignGroupId | RN |
+-----------+-----------+-----------+--------------------------------------+----+
| 18288 | 3974 | 63 | F18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 1 |
| 18288 | 4096 | 107 | F18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 2 |
| 18288 | 7224 | 66 | F18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 3 |
| 18288 | 4842 | 66 | A18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 1 |
| 18289 | 7325 | 66 | 90C224D9-2514-4294-8DEC-D3EE16EC2D00 | 1 |
| 18289 | 3975 | 63 | 90C224D9-2514-4294-8DEC-D3EE16EC2D00 | 2 |
+-----------+-----------+-----------+--------------------------------------+----+
在这种情况下,我有两个不同的 LegacyKey
但结果是错误的,因为它 returns 所有 RN
相同 DesignGroupId
而我只想要最后一个。期望结果是:
+-----------+-----------+-----------+--------------------------------------+----+
| LegacyKey | DesignKey | StatusKEy | DesignGroupId | RN |
+-----------+-----------+-----------+--------------------------------------+----+
| 18288 | 7224 | 66 | F18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 3 |
| 18288 | 4842 | 66 | A18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 1 |
| 18289 | 3975 | 63 | 90C224D9-2514-4294-8DEC-D3EE16EC2D00 | 2 |
+-----------+-----------+-----------+--------------------------------------+----+
如你所见,我得到了 DesignGroupId
的最后一个 RN
,但它们在同一个 LegacyKey
我不希望在子查询中出现 GROUP BY
。我希望在外部查询中进行过滤:
WITH CTE2 (LegacyKey, DesignKey, StatusKey, DesignGroupId, RN) AS (
SELECT LegacyKey, DesignKey, StatusKey, DesignGroupId,
ROW_NUMBER() OVER (PARTITION BY [LegacyKey], [DesignGroupId] ORDER BY [DesignGroupId] DESC) AS seqnum
FROM @DeadDesigns
)
SELECT *
FROM CTE2
WHERE seqnum = 1;
请注意,我将 ORDER BY
更改为 DESC
- 当您想要某些东西的 "last" 或 "most recent" 时,这很典型。
我有这样的查询:
DECLARE @DeadDesigns AS TABLE(
LegacyKey INT
,DesignKey INT
,StatusKey INT
,DesignGroupId UNIQUEIDENTIFIER
)
INSERT INTO @DeadDesigns
SELECT [P].[LegacyKey],[D].DesignKey, [D].[StatusKey], [D].[DesignGroupId] FROM Project AS P
INNER JOIN DesignGroup AS DG ON P.ProjectKey = DG.ProjectKey
INNER JOIN Design AS D ON DG.DesignGroupId = D.DesignGroupId
WHERE [D].[StatusKey] = 67 --DEAD
;WITH CTE2 (LegacyKey, DesignKey, StatusKey, DesignGroupId, RN) AS (
SELECT LegacyKey, DesignKey, StatusKey, DesignGroupId,
ROW_NUMBER() OVER (PARTITION BY [LegacyKey], [DesignGroupId] ORDER BY [DesignGroupId]) AS RN
FROM @DeadDesigns
GROUP BY [DesignGroupId],[LegacyKey],DesignKey,StatusKey
)
SELECT * FROM CTE2
结构是:
一个LegacyKey
可以有多个DesignGroupId
,一个DesignGroupId
可以有多个DesignKey
Objective 是将每个 DesignGroupId
中的最后一个 DesignKey
从项目
错误结果:
+-----------+-----------+-----------+--------------------------------------+----+
| LegacyKey | DesignKey | StatusKEy | DesignGroupId | RN |
+-----------+-----------+-----------+--------------------------------------+----+
| 14002 | 2416 | 67 | 1A07C80E-E5E2-45F0-A5D2-419BAF3DC106 | 1 |
| 14002 | 2819 | 70 | 1A07C80E-E5E2-45F0-A5D2-419BAF3DC106 | 2 |
+-----------+-----------+-----------+--------------------------------------+----+
这是错误的,因为它是相同的 DesignGroupId,如果 DesignGroupId 不同,则结果是正确的。
详细愿望结果:
我想用 DesignGroupId
分隔它,所以如果有两个不同的 DesignGroupId
并且它们相同 LegacyKey
第二行的 RN
应该是 2.. 如果我们有 3 DesignGroupId
但第二行的相同 LegacyKey
RN
应该是 3 等等。最后我想在每个 LegacyKey
DesignGroupId
中获取所有 last DesignKey
我做错了什么?
另一个例子
+-----------+-----------+-----------+--------------------------------------+----+
| LegacyKey | DesignKey | StatusKEy | DesignGroupId | RN |
+-----------+-----------+-----------+--------------------------------------+----+
| 18288 | 3974 | 63 | F18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 1 |
| 18288 | 4096 | 107 | F18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 2 |
| 18288 | 7224 | 66 | F18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 3 |
| 18288 | 4842 | 66 | A18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 1 |
| 18289 | 7325 | 66 | 90C224D9-2514-4294-8DEC-D3EE16EC2D00 | 1 |
| 18289 | 3975 | 63 | 90C224D9-2514-4294-8DEC-D3EE16EC2D00 | 2 |
+-----------+-----------+-----------+--------------------------------------+----+
在这种情况下,我有两个不同的 LegacyKey
但结果是错误的,因为它 returns 所有 RN
相同 DesignGroupId
而我只想要最后一个。期望结果是:
+-----------+-----------+-----------+--------------------------------------+----+
| LegacyKey | DesignKey | StatusKEy | DesignGroupId | RN |
+-----------+-----------+-----------+--------------------------------------+----+
| 18288 | 7224 | 66 | F18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 3 |
| 18288 | 4842 | 66 | A18320D8-C7A8-4A2A-AFDE-38A483C24E81 | 1 |
| 18289 | 3975 | 63 | 90C224D9-2514-4294-8DEC-D3EE16EC2D00 | 2 |
+-----------+-----------+-----------+--------------------------------------+----+
如你所见,我得到了 DesignGroupId
的最后一个 RN
,但它们在同一个 LegacyKey
我不希望在子查询中出现 GROUP BY
。我希望在外部查询中进行过滤:
WITH CTE2 (LegacyKey, DesignKey, StatusKey, DesignGroupId, RN) AS (
SELECT LegacyKey, DesignKey, StatusKey, DesignGroupId,
ROW_NUMBER() OVER (PARTITION BY [LegacyKey], [DesignGroupId] ORDER BY [DesignGroupId] DESC) AS seqnum
FROM @DeadDesigns
)
SELECT *
FROM CTE2
WHERE seqnum = 1;
请注意,我将 ORDER BY
更改为 DESC
- 当您想要某些东西的 "last" 或 "most recent" 时,这很典型。