对任何可以提供帮助的人。我不是T-SQL的专家所以我来这里是为了一些指导。我有一个公用表表达式,其中两列具有用户ID。我需要根据这些ID从用户表中获取名称,以便显示名称。使用我的CTE和INNER JOINS会减少我的记录数,我不知道为什么。
更新我很抱歉我应该更清楚。没错。出乎意料的结果。在加入之前,我返回了135条记录。通过连接我得到6。
这是SQL:
;WITH CTE
AS (SELECT *,
ROW_NUMBER() OVER (PARTITION BY [ChangeRequests].[F0]
ORDER BY [ChangeRequests].[F8] DESC) AS RN
FROM [S37] AS [ChangeRequests])
SELECT [CTE].[F0] AS [ID]
,[CTE].[F8] AS [RevisionNumber]
,[CTE].[F18] AS [ChangeNumber]
,[CTE].[F19] AS [Synopsis]
--,[CTE].[F30] AS [Responsibility]
,[CTE].[F32] AS [Description]
,(CASE [CTE].[F42] WHEN 0 THEN NULL ELSE dbo.ConvertSTTimestamp([CTE].[F42]) END) AS [EnteredOn]
,[CTE].[F51] AS [Usr_Project]
--,[CTE].[F52] AS [Usr_CoResponsibility]
,[CTE].[F59] AS [Usr_ReportedBy]
,[CTE].[F61] AS [Usr_StarFlowStatus]
,[Users1].[F7] AS [Responsibility]
,[Users2].[F7] AS [CoResponsibility]
FROM CTE
INNER JOIN [S3] [Users1] ON [Users1].[F0] = [CTE].[F30]
INNER JOIN [S3] [Users2] ON [Users2].[F0] = [CTE].[F52]
WHERE (RN = 1)
如何根据我的2个用户ID列进行加入以获取用户名?
谢谢!
答案 0 :(得分:4)
将INNER JOIN
更改为LEFT JOIN
,然后告诉我们你得到了什么,以及它是否更接近你想要/期望的......