结果从公共ID连接多个列

时间:2018-10-26 09:11:08

标签: sql sql-server sql-server-2016

我最终试图建立一个视图

我有2个表,在一个事务中发布的用户名和ID在另一个事务中进行了修改

Table A 
ID,Username

Table B 
Date ,User ID , Modified UserID , Created User ID,INVtotal,InVDesc

Select a.Username,Date ,User ID , Modified UserID , Created User ID,INVtotal,InVDesc
   from Table B
   join 
   Table A  on a.ID = User ID

上面的结果给我很好的结果,但是我想让用户名而不是ID出现在Modified UserID和Created User列中,因为随着更改的发生,它们可能会有所不同。

谢谢

样本数据

ID  Username                
1   May                 
2   Jon             
3   Sam             
4   Eric                

Date      UserID    ModifedUserID   CreatedUserID   INVTotal InVDesc
26/10/2018  1        2                 1             100    A123
26/10/2018  1        3                 1             200    A124
26/10/2018  2        4                 2             100    A125
26/10/2018  2                          2             200    A126
26/10/2018  4        1                 4             100    A127
26/10/2018  3        1                 3             200    A128

预期结果

Date        UserID   ModifedUserID  CreatedUserID    INVTotal   InVDesc
26/10/2018  May      Jon            May              100    A123

3 个答案:

答案 0 :(得分:1)

希望我能正确理解你

SELECT
  b.Date,
  --b.UserID,
  u1.Username,
  --b.ModifiedUserID,
  u2.Username ModifiedUsername,
  --b.CreatedUserID,
  u3.Username CreatedUsername,
  b.INVtotal,
  b.InVDesc
FROM TableB b
JOIN TableA u1 ON b.UserID=u1.ID
LEFT JOIN TableA u2 ON b.ModifiedUserID=u2.ID
LEFT JOIN TableA u3 ON b.CreatedUserID=u3.ID

如果b.UserID可以是NULL,那么您还需要将LEFT JOIN用于TableA u1

答案 1 :(得分:0)

你在这里

CREATE TABLE T1
    ([ID] int, [Username] varchar(4))
;

INSERT INTO T1
    ([ID], [Username])
VALUES
    (1, 'May'),
    (2, 'Jon'),
    (3, 'Sam'),
    (4, 'Eric')
;


CREATE TABLE T2
    ([Date] varchar(10), [UserID] int, [ModifedUserID] int, [CreatedUserID] int, [INVTotal InVDesc] varchar(8))
;

INSERT INTO T2
    ([Date], [UserID], [ModifedUserID], [CreatedUserID], [INVTotal InVDesc])
VALUES
    ('26/10/2018', 1, 2, 1, '100 A123'),
    ('26/10/2018', 1, 3, 1, '200 A124'),
    ('26/10/2018', 2, 4, 2, '100 A125'),
    ('26/10/2018', 2, 2, 200, 'A126'),
    ('26/10/2018', 4, 1, 4, '100 A127'),
    ('26/10/2018', 3, 1, 3, '200 A128')
;

SELECT T1.UserName,
       T11.UserName As ModifiedBy,
       T111.UserName As CreatedBy,
       T2.[Date],
       T2.[INVTotal InVDesc]
FROM T1 INNER JOIN T2 ON T1.ID = T2.UserID
        INNER JOIN T1 T11 ON T2.ModifedUserID = T11.ID
        INNER JOIN T1 T111 ON T2.CreatedUserID = T111.ID
WHERE T2.ModifedUserID = 2;

答案 2 :(得分:0)

如上所述,使用 JOIN 已经有两种解决方案,但是我在这里发布了不同的解决方案-使用 FUNCTION

创建一个用户定义的函数,该函数根据提供的用户ID返回用户名。

CREATE FUNCTION dbo.ufn_GetUserName (@userID INT)
RETURNS VARCHAR(10)
AS
BEGIN
     DECLARE @username VARCHAR(10) = ''
     SELECT @username = Username FROM TableA WHERE ID = @userID
     RETURN @username
END

使用此功能来获取有关用户ID的用户名。这样,您可以避免查询中出现多个联接。

SELECT 
    DATE,
    dbo.ufn_GetUserName(UserID) AS UserID,
    dbo.ufn_GetUserName(ModifiedUserID) AS ModifiedUserID, 
    dbo.ufn_GetUserName(CreatedUserID) AS CreatedUserID,
    INVtotal,
    InVDesc
FROM TableB

谢谢, 桑迪