如何在SQL中建立一对多关系?我有一个查询,它打印给定的输出:https://www.screencast.com/t/II0h2YXRi。现在我已经完成了想要打印具有相同值的Name列的操作。
DECLARE @agv NVARCHAR(max);
DECLARE @Con NVARCHAR(max);
SET @agv = '';
SET @Con = '';
SELECT DISTINCT dbo.bsContract.ContractNumber,Advertiser.OrganizationName AS Advertiser,
JobName AS Campaign,bsContract.FromDate AS StartDate,
Advertiser.OrganizationName AS Client,
(ISNULL(cli.FirstName, '') + ' ' + cli.LastName) AS ClientRep,
c.OrganizationName AS Vendor,
(ISNULL(con.FirstName, '') + ' ' + con.LastName) AS 'VendorRep'
FROM dbo.bsDocument
INNER JOIN Opportunity ON bsDocument.OpportunityID = Opportunity.OpportunityID
INNER JOIN Organization AS Advertiser ON Advertiser.OrganizationID = Opportunity.OrganizationID
INNER JOIN Contact AS UploadContact ON UploadContact.ContactID = bsDocument.UploadedBy
LEFT JOIN Organization ON bsDocument.OrganizationID = Organization.OrganizationID
LEFT JOIN dbo.bsContractLineItemV2 ON bsContractLineItemV2.ContractNumber = bsDocument.ModuleID
AND bsContractLineItemV2.VendorOrgID = bsDocument.OrganizationID
LEFT JOIN dbo.bsContract ON bsContract.ContractNumber = bsDocument.ModuleID
LEFT JOIN Contact AS CompanyDocContact ON CompanyDocContact.ContactID = bsContractLineItemV2.VendorContactID
INNER JOIN dbo.Organization AS c ON c.OrganizationID = dbo.bsContractLineItemV2.VendorOrgID
LEFT JOIN dbo.Contact AS con ON VendorContactID = con.ContactID
LEFT JOIN dbo.Contact AS cli ON bsContract.ContactID = cli.ContactID
WHERE MONTH(bsContract.FromDate) = 3 AND YEAR(bsContract.FromDate) = 2019
AND Advertiser.OrganizationName LIKE '%'+@agv+'%'
AND bsContract.ContractNumber LIKE '%'+@Con+'%'
答案 0 :(得分:0)
这不是您可以或应该在SQL中执行的操作。如果您具有处理查询结果集并将其输出到文件的代码,则可以在其中进行处理。或者,在导出到Excel之后,您可以应用条件格式来实现外观,如下所述:https://stackoverflow.com/a/9174254/3250365。