2017-02-08 113 views
0

自從使用SSRS和SQL以來已經有好幾年了,所以我對此補救措施和措辭不佳的問題事先表示歉意。將多個表列添加到多個表中

我有一個表,事件,其中包括2列,開瓶器和所有者(都是整數)。這些列與用戶表相關,我想從中檢索用戶名。我可以加入這些表格,但是我如何才能獲取用戶名作爲首選項和所有者,以便將其包含在我的報告中?

SELECT 
    TST_INCIDENT.OPENER_ID AS Logged_By, 
    TST_INCIDENT.OWNER_ID AS Assignee, 
    TST_INCIDENT.INCIDENT_ID AS Defect_ID, 
    TST_INCIDENT.NAME AS Defect_Title, 
    TST_INCIDENT.DESCRIPTION AS Defect_Desc, 
    TST_USER.USER_NAME 
FROM 
    TST_USER 
INNER JOIN 
    TST_INCIDENT ON TST_USER.USER_ID = TST_INCIDENT.OPENER_ID 
       AND TST_USER.USER_ID = TST_INCIDENT.OWNER_ID 
+0

是什麼問題?請提供樣本數據和DDL報表 –

回答

0

您將爲每個ID加入一次用戶表。如果ID可能丟失,請將它們更改爲左連接。

SELECT 
    TST_INCIDENT.OPENER_ID AS Logged_By 
    ,TST_INCIDENT.OWNER_ID AS Assignee 
    ,TST_INCIDENT.INCIDENT_ID AS Defect_ID 
    ,TST_INCIDENT.NAME AS Defect_Title 
    ,TST_INCIDENT.DESCRIPTION AS Defect_Desc 
    ,A.USER_NAME as OPENER_USER_NAME 
    ,B.USER_NAME as OWNER_USER_NAME 
FROM TST_INCIDENT 
INNER JOIN TST_USER A ON A.USER_ID = TST_INCIDENT.OPENER_ID 
INNER JOIN TST_USER B ON B.USER_ID = TST_INCIDENT.OWNER_ID 
+0

好悲傷,那太簡單了。只要看到選擇,它慢跑我的記憶。非常感謝! – Masintenn