获取父表的最后一行

时间:2019-02-22 17:57:19

标签: sql sql-server tsql

我有一些项目,里面有设计。因此,一个项目可以有多个设计。

项目表:

+------------+------+
| ProjectKey | Name |
+------------+------+

ProjectDesignTable:

+------------+-----------+
| ProjectKey | DesignKey |
+------------+-----------+

设计表:

+-----------+------+--------------+----------------+-----------------+
| DesignKey | Name | DesignNumber | DesignTypeGuid | ParentDesignKey |
+-----------+------+--------------+----------------+-----------------+

所以我有一个这样的选择查询:

SELECT
      *
      FROM [project] AS [p]
          INNER JOIN [ProjectDesign] AS [pd] ON [p].[ProjectKey] = [pd].[ProjectKey]
          INNER JOIN [Design] AS [d] ON [d].[DesignKey] = [pd].[DesignKey]
      WHERE [d].[DesignTypeGuid] = '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D'

我在每个项目中得到的所有设计都有DesignTypeGuid: '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D'

我想要实现的是在每个项目的第一个设计键之后获取行,例如,如果我有此行:

    +------------+--------------+-----------+----------------------------------------+-----------------+
| ProjectKey | DesignNumber | DesignKey |               DesignType               | ParentDesignKey |
+------------+--------------+-----------+----------------------------------------+-----------------+
|        152 |            1 |       187 | '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D' | NULL            |
|        152 |            2 |      1316 | '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D' | NULL            |
|       1171 |            1 |      1206 | '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D' | NULL            |
|       1171 |            2 |      1317 | '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D' | NULL            |
|       1171 |            3 |      1401 | '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D' | NULL            |
+------------+--------------+-----------+----------------------------------------+-----------------+

我渴望得到的结果是:

 152 |            2 |      1316 | '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D'
1171 |            2 |      1317 | '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D' 
1171 |            3 |      1401 | '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D'

一旦得到这个信息,我想用每个项目I.E的第一个designKey更新该行的ParentDesignKey列:

152 |            2 |      1316 | '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D'

应该为第一个设计的"187"(DesignKey)更新ParentKey

我该如何实现?问候

3 个答案:

答案 0 :(得分:1)

这是一个交叉应用解决方案:

update d set ParentDesignKey=firstDesignKey
from Design d
cross apply
(
    select top 1 Designkey as firstDesignKey
    from Design dfirst
    inner join ProjectDesign AS pd ON dfirst.DesignKey = [pd].[DesignKey]
    where d.DesignKey = [pd].[DesignKey]
    order by dfirst.DesignNumber asc
)q

答案 1 :(得分:1)

假设我理解了这个问题,则可以使用 lag first_value来获取{{1} }与当前行相同,但DesignKey较低:

ProjectKey

对于您发布的样本数据,此查询应为您提供以下结果集:

DesignNumber

另外,请注意,由于您在任何地方都不使用其中的任何列,因此实际上不需要加入SELECT ProjectKey, DesignNumber, DesignKey, DesignType, FIRST_VALUE(DesignKey) OVER(PARTITION BY ProjectKey ORDER BY DesignNumber) AS ParentDesignKey FROM [ProjectDesign] AS [pd] INNER JOIN [Design] AS [d] ON [d].[DesignKey] = [pd].[DesignKey] WHERE [d].[DesignTypeGuid] = '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D' 表。

答案 2 :(得分:0)

SELECT p.ProjectKey, max(DesignNumber), d.DesignKey, DesignType 
FROM [project] AS [p]
      INNER JOIN [ProjectDesign] AS [pd] ON [p].[ProjectKey] = [pd].[ProjectKey]
      INNER JOIN [Design] AS [d] ON [d].[DesignKey] = [pd].[DesignKey]
WHERE [d].[DesignTypeGuid] = '54FBBC23-CB9A-4311-9D7F-0DD7A774F33D'
GROUP BY p.ProjectKey, d.DesignKey, DesignType