编辑我最初的问题包括示例数据、我当前的查询和我试图合并的聚合,以便每个 ContactCd 不会创建新行。 我不知道如何使我的示例不那么复杂。 如果我将其简化,是否会删除查询所需的所有连接?
SELECT
jobNew_HeaderFile.JobCd,
GRP = max( case when jobCtrl_Rollup.RollupCatCd = 'GRP' then jobCtrl_Rollup.RollupCd end ),
REG = max( case when jobCtrl_Rollup.RollupCatCd = 'REG' then jobCtrl_Rollup.RollupCd end ),
' ' AS 'JOB_1',
' ' AS 'FIELD OPS MANAGER',
' ' AS 'NATIONAL OPS MANAGER',
DIV = max( case when jobCtrl_Rollup.RollupCatCd = 'DIV' then jobCtrl_Rollup.RollupCd end ),
jobCtrl_Company.Company,
jobNew_HeaderFile.JobName AS LOCATION,
jobNew_SiteDetail.JobAddr1 AS Addr1,
jobNew_SiteDetail.JobAddr2 AS Addr2,
jobNew_SiteDetail.JobCity AS City,
jobNew_SiteDetail.JobState AS State,
jobNew_SiteDetail.JobZip1 AS Zip,
jobNew_SiteDetail_Contacts.ContactCd,
jobNew_SiteDetail_Contacts.FirstName AS FNAME1,
jobNew_SiteDetail_Contacts.LastName AS LNAME1,
jobNew_SiteDetail_Contacts.Title AS TITLE1,
jobNew_SiteDetail_Contacts.FirstName AS FNAME2,
jobNew_SiteDetail_Contacts.LastName AS LNAME2,
jobNew_SiteDetail_Contacts.Title AS TITLE2,
jobNew_SiteDetail_Contacts.FirstName AS FNAME3,
jobNew_SiteDetail_Contacts.LastName AS LNAME3,
jobNew_SiteDetail_Contacts.Title AS TITLE3,
jobNew_SiteDetail_Contacts.Phone_wk,
jobNew_SiteDetail_Contacts.Phone_cell,
jobNew_SiteDetail_Contacts.email AS Email,
jobNew_SiteDetail.ICCPhone AS ICC_Phone,
jobNew_SiteDetail.ICCPhoneNote AS ICC_Phone_Note,
jobNew_SiteDetail.Email AS ICC_Email,
mstClients.ClientCd,
mstClients.ClientName,
jobCtrl_Rollup.RollupCatCd,
jobCtrl_Rollup.RollupCd
FROM jobNew_HeaderFile
INNER JOIN jobNew_SiteDetail ON jobNew_HeaderFile.JobCd = jobNew_SiteDetail.JobCd
INNER JOIN jobNew_SiteDetail_Contacts ON jobNew_HeaderFile.JobCd = jobNew_SiteDetail_Contacts.JobCd
INNER JOIN jobCtrl_Company ON jobNew_HeaderFile.JobCd = jobCtrl_Company.JobCd
INNER JOIN mstClients ON jobNew_SiteDetail.ClientCd = mstClients.ClientCd
INNER JOIN jobCtrl_Rollup ON jobNew_HeaderFile.JobCd = jobCtrl_Rollup.JobCd
WHERE
jobNew_HeaderFile.JobCd = 990
--AND jobNew_SiteDetail.ActiveStatus = '1'
AND jobCtrl_Rollup.CurrentYN = '1'
GROUP BY
jobNew_HeaderFile.JobCd,
jobCtrl_Company.Company,
jobNew_HeaderFile.JobName,
jobNew_SiteDetail.JobAddr1,
jobNew_SiteDetail.JobAddr2,
jobNew_SiteDetail.JobCity,
jobNew_SiteDetail.JobState,
jobNew_SiteDetail.JobZip1,
jobNew_SiteDetail_Contacts.ContactCd,
jobNew_SiteDetail_Contacts.FirstName,
jobNew_SiteDetail_Contacts.LastName,
jobNew_SiteDetail_Contacts.Title,
jobNew_SiteDetail_Contacts.Phone_wk,
jobNew_SiteDetail_Contacts.Phone_cell,
jobNew_SiteDetail_Contacts.email,
jobNew_SiteDetail.ICCPhone,
jobNew_SiteDetail.ICCPhoneNote,
jobNew_SiteDetail.Email,
mstClients.ClientCd,
mstClients.ClientName,
jobCtrl_Rollup.RollupCatCd,
jobCtrl_Rollup.RollupCd
我试图包含的汇总是:
Select JobCd
,FName1 = max( case when RN=1 then FName end)
,LName1 = max( case when RN=1 then LName end)
,FName2 = max( case when RN=2 then FName end)
,LName2 = max( case when RN=2 then LName end)
,FName3 = max( case when RN=3 then FName end)
,LName3 = max( case when RN=3 then LName end)
From (
Select JobCd
,RN = row_number() over (partition by JobCd order by [ContactCd])
,FName = parsename(replace([FirstName],' ','.'),1)
,LName = parsename(replace([LastName],' ','.'),1)
From jobNew_SiteDetail_Contacts
) A
Group By JobCd
想要的结果:
工作CD | F名称1 | L名称1 | F名称2 | L名称2 | F名称3 | L名称3 |
---|---|---|---|---|---|---|
990 | 贝基 | 史密斯 | 杰弗里 | 梅森 | 基督教 | 福腾伯格 |
这是无法回答的,比如我们不知道Job表和其他表之间有什么关系。是1-多等吗
但是为什么不先进行所需的聚合,然后再次连接其余表呢?这会简化您的查询。
类似:
select * from (
select ID, MAX(...) from
Sometable GROUP BY ID
) x
INNER JOIN SomeDetails sd
ON sd.ID = x.ID
INNER JOIN SomeMoreDetails sd2 ON sd2.ID = x.ID`
... rince and repeat
这样您就不需要聚合十亿个字段。
此外,您的查询示例有:
jobNew_HeaderFile.JobName AS LOCATION, jobNew_SiteDetail.JobAddr1 AS Addr1, jobNew_SiteDetail.JobAddr2 AS Addr2, jobNew_SiteDetail.JobCity AS City, jobNew_SiteDetail.JobState AS State, jobNew_SiteDetail.JobZip1 AS Zip,
您认为您必须指定问题中的所有列吗?在我看来,如果你的问题只是:
jobNew_HeaderFile.JobName AS LOCATION