我有3张桌子:
表1:“ vw_dimserver”:[ServerKey,ServerNm,ServerCurrentAssignedToNm]例如:[1234,hasgapp0108,Aya]
table2“ vw_DimWorker”:[WorkerEmailNm,WorkerIdsid]。例如:[Aya,akhate],[Genna,gshysh],[mish,mishka](就像名称和用户名一样)
表3“ vw_FactHardwareAssetContact”:[ServerKey,HardwareAssetWorkerKey,HardwareAssetContactTypeNm]。例如:[1234,Aya,support] [1234,Genna,Customer] [1234,Mish,patch](如果是支持人员,客户或补丁程序,则为服务器密钥,联系人姓名和联系人类型)
我需要将输出放在一行中: [ServerNm,ServerCurrentAssignedToNm的WorkerIdsid,客户的WorkerIdsid,支持的WorkerIdsid] [hasgapp0108,akhate,gshysh,mishka]
现在,我需要加入用户名表以获取每个用户的用户名。 我是通过完全外部联接做到的。
代码是:
选择 vw_dimserver.ServerNm作为主机名, (vw_DimWorker1.WorkerIdsid)作为用户名, (vw_DimWorker2.WorkerIdsid)作为[主要支持联系人], (vw_DimWorker3.WorkerIdsid)作为[主要客户联系人]
from vw_dimserver
inner join vw_FactHardwareAssetContact on vw_dimserver.ServerKey = vw_FactHardwareAssetContact.HardwareAssetKey
inner join vw_DimWorker as vw_DimWorker1 on vw_DimWorker1.WorkerEmailNm = vw_dimserver.ServerCurrentAssignedToNm
full outer join vw_DimWorker as vw_DimWorker2 on CONVERT(varchar(11),vw_FactHardwareAssetContact.HardwareAssetWorkerKey ) = vw_DimWorker2.workerWWID and vw_FactHardwareAssetContact.HardwareAssetContactTypeNm = 'Support' and vw_FactHardwareAssetContact.HardwareAssetPrimaryInd = '1' and( vw_FactHardwareAssetContact.HardwareAssetWorkerKey is not NULL)
full outer join vw_DimWorker as vw_DimWorker3 on CONVERT(varchar(11),vw_FactHardwareAssetContact.HardwareAssetWorkerKey ) = vw_DimWorker3.workerWWID and vw_FactHardwareAssetContact.HardwareAssetContactTypeNm = 'Customer' and vw_FactHardwareAssetContact.HardwareAssetPrimaryInd = '1' and( vw_FactHardwareAssetContact.HardwareAssetWorkerKey is not NULL)
where vw_dimserver.ServerNm ='hasgapp0108' and (vw_DimWorker2.workerWWID is not null or vw_DimWorker3.workerWWID is not null ) and ( ServerCurrentSubStatusCd != 'duplicate_error' or ServerCurrentSubStatusCd is NULL )
输出为两行:
[主机名用户名支持客户]
[hasgapp0108 akhate NULL gshysh],
[hasgapp0108 akhate akhate NULL]
并且我需要将其排成一行:
[主机名用户名支持客户]
[hasgapp0108 akhate akhate gshysh]
我的经理不接受该解决方案:
select max(username1),max(username2)...
我需要通过联接来实现。
答案 0 :(得分:0)
尝试使用内部联接和group_concat:
SELECT vw_dimserver.ServerNm as hostname , group_concat(vw_DimWorker1.WorkerIdsid) as username, group_concat(vw_DimWorker2.WorkerIdsid) as [Primary Support Contact], group_concat(vw_DimWorker3.WorkerIdsid) as [Primary Customer Contact]
from vw_dimserver
inner join vw_FactHardwareAssetContact on vw_dimserver.ServerKey = vw_FactHardwareAssetContact.HardwareAssetKey
inner join vw_DimWorker as vw_DimWorker1 on vw_DimWorker1.WorkerEmailNm = vw_dimserver.ServerCurrentAssignedToNm
full outer join vw_DimWorker as vw_DimWorker2 on CONVERT(varchar(11),vw_FactHardwareAssetContact.HardwareAssetWorkerKey ) = vw_DimWorker2.workerWWID and vw_FactHardwareAssetContact.HardwareAssetContactTypeNm = 'Support' and vw_FactHardwareAssetContact.HardwareAssetPrimaryInd = '1' and( vw_FactHardwareAssetContact.HardwareAssetWorkerKey is not NULL)
full outer join vw_DimWorker as vw_DimWorker3 on CONVERT(varchar(11),vw_FactHardwareAssetContact.HardwareAssetWorkerKey ) = vw_DimWorker3.workerWWID and vw_FactHardwareAssetContact.HardwareAssetContactTypeNm = 'Customer' and vw_FactHardwareAssetContact.HardwareAssetPrimaryInd = '1' and( vw_FactHardwareAssetContact.HardwareAssetWorkerKey is not NULL)
where vw_dimserver.ServerNm ='hasgapp0108' and (vw_DimWorker2.workerWWID is not null or vw_DimWorker3.worker
group by vw_dimserver.ServerNm
答案 1 :(得分:0)
如果只需要匹配结果,则可以使用内部联接
SELECT
s.ServerKey,
s.ServerNm,
group_concat(w.WorkerIdsid)
FROM vw_dimserver s
INNER JOIN vw_FactHardwareAssetContact c
ON s.ServerKey = c.ServerKey
INNER JOIN vw_DimWorker w
ON c.HardwareAssetWorkerKey = w.WorkerEmailNm
GROUP BY s.ServerKey,
s.ServerNm
或者如果没有所有匹配项,也可以使用左联接
SELECT
s.ServerKey,
s.ServerNm,
group_concat(w.WorkerIdsid)
FROM vw_dimserver s
LEFT JOIN vw_FactHardwareAssetContact c
ON s.ServerKey = c.ServerKey
LEFT JOIN vw_DimWorker w
ON c.HardwareAssetWorkerKey = w.WorkerEmailNm
GROUP BY s.ServerKey,
s.ServerNm
答案 2 :(得分:0)
选择vw_dimserver.ServerNm作为主机名,(vw_DimWorker1.WorkerIdsid)作为用户名,(vw_DimWorker2.WorkerIdsid)作为[主要支持联系人],(vw_DimWorker3.WorkerIdsid)作为[主要客户联系人]
来自vw_dimserver
完整外部联接vw_FactHardwareAssetContact作为vw_FactHardwareAssetContact1 在(vw_dimserver.ServerKey = vw_FactHardwareAssetContact1.HardwareAssetKey和vw_FactHardwareAssetContact1.HardwareAssetContactTypeNm ='Support'和vw_FactHardwareAssetContact1.HardwareAssetPrimaryInd ='1')
在CONVERT上作为vw_DimWorker1的完整外部联接vw_DimWorker(varchar(11),vw_FactHardwareAssetContact1.HardwareAssetWorkerKey)= vw_DimWorker1.workerWWID
在assings.WorkerEmailNm = vw_dimserver.ServerCurrentAssignedToNm上作为完整外部联接vw_DimWorker
完整外部联接vw_FactHardwareAssetContact作为vw_FactHardwareAssetContact2(vw_dimserver.ServerKey = vw_FactHardwareAssetContact2.HardwareAssetKey和vw_FactHardwareAssetContact2.HardwareAssetContactTypeNm ='Customer'和vwAssetHardwared。
在CONVERT上作为vw_DimWorker2的完整外部联接vw_DimWorker(varchar(11),vw_FactHardwareAssetContact2.HardwareAssetWorkerKey)= vw_DimWorker2.workerWWID
完整的外部将vw_FactHardwareAssetContact作为vw_FactHardwareAssetContact11加入(vw_dimserver.ServerKey = vw_FactHardwareAssetContact11.HardwareAssetKey和vw_FactHardwareAssetContact11.HardwareAssetContactTypeNm ='Support'和vw_FactHard
在CONVERT上作为vw_DimWorker11的完整外部联接vw_DimWorker(varchar(11),vw_FactHardwareAssetContact11.HardwareAssetWorkerKey)= vw_DimWorker11.workerWWID
在(vw_dimserver.ServerKey = vw_FactHardwareAssetContact22.HardwareAssetKey和vw_FactHardwareAssetContact22.HardwareAssetContactTypeNm ='Customer'HardwareAsetd'Hardware's'HardwareAssetd 上输入vw_FactHardwareAssetContact22。
完全外部联接vw_DimWorker作为CONVERT上的vw_DimWorker22(varchar(11),vw_FactHardwareAssetContact22.HardwareAssetWorkerKey)= vw_DimWorker22.workerWWID