To anyone who can help. I am not an expert at T-SQL so I am coming here for some guidance. I have a Common Table Expression with two columns that have user id’s. I need to get the name from the user table based on those ID’s so I display the name. Using my CTE with INNER JOINS reduce my record count and I do not know why.
UPDATE I am sorry I should have been more clear. No error. Unexpected results. Before the joins I was returning 135 records. With the joins I get 6.
Here is the 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)
How do I do a JOIN based on my 2 user ID columns to get the user name?
Thanks!
Change the
INNER JOINs toLEFT JOINs, and tell us what you get, and if it’s any closer to what you want/expect…