将行SQL SQL特定组合在一起

时间:2014-01-14 22:00:25

标签: sql sql-server rows

我需要一些帮助来开发一个将同一个客户端的行组合在一起的查询。基本上我有一个客户,有税务经理,审计经理,客户经理等。

到目前为止,我使用的代码我能够提取信息但是它为每种类型的管理器吐出了同一个客户端的几行

图片上的示例(包含代码和结果)。在下面我添加了一个链接,它不会让我直接添加图片。

Here is the link of the screenshot to get a better idea, it wouldn't let me add a picture here!!

代码如下:

select ocr.staffcode as OCR, clientcode, clientname, 
case when cs.ServIndex = 'TAXCOMP' then tax.StaffCode else '-' end as 'Tax',
case when cs.ServIndex = 'AUDIT' then audit.staffcode else '-' end as 'Audit',
case when cs.ServIndex = 'REVIEW' then review.staffcode else '-' end as 'Review',
case when cs.ServIndex = 'COMP' then comp.staffcode else '-' end as 'Comp',
case when cs.ServIndex = 'RandE' then rande.staffcode else '-' end as 'R&E',
case when cs.ServIndex = 'ACCTG' then acctg.staffcode else '-' end as 'Acctg',
case when cs.ServIndex = 'VMA' then vma.staffcode else '-' end as 'BV',
case when cs.ServIndex = 'LIT' then lit.staffcode else '-' end as 'Lit',
case when cs.ServIndex = 'FORENSIC' then lit.staffcode else '-' end as 'Forensics',
case when cs.ServIndex = 'CONS' then lit.staffcode else '-' end as 'Consulting'
from tblEngagement e
inner join tblClientServices cs on cs.ContIndex = e.contindex
left outer join tblStaff ocr on ocr.StaffIndex = e.ClientPartner
left outer join tblStaff tax on tax.StaffIndex = cs.ServManager
left outer join tblStaff audit on audit.StaffIndex = cs.ServManager
left outer join tblStaff review on review.StaffIndex = cs.ServManager
left outer join tblStaff comp on comp.StaffIndex = cs.ServManager
left outer join tblStaff rande on rande.StaffIndex = cs.ServManager
left outer join tblStaff acctg on acctg.StaffIndex = cs.ServManager
left outer join tblStaff vma on vma.StaffIndex = cs.ServManager
left outer join tblStaff lit on lit.StaffIndex = cs.ServManager
left outer join tblStaff fore on fore.staffindex = cs.ServManager
where e.ContIndex < 900000 and cs.ServActPos = 1 and ClientStatus in ('ACTIVE','SUSPENDED') 
order by ClientCode

我希望能够将所有相同的OCR,Clietcode,客户名称中的所有内容组合成一行显示税务经理,审计经理,审核经理等...如果没有经理其中一个只是在我的例子中显示“ - ”。我一直在读几个地方并试图自己做但没有运气。任何人都可以帮助指导我朝正确的方向发展吗?

2 个答案:

答案 0 :(得分:0)

尝试按客户端进行分组,从而消除重复项,最大化大小写列,然后用' - '填充NULL。但是,以下代码每个类别仅显示一个经理。那么应该有两个审计经理,只有一个将由此返回:

select ocr.staffcode as OCR, clientcode, clientname, 
coalesce(max(case when cs.ServIndex = 'TAXCOMP' then tax.StaffCode end),'-') as 'Tax',
coalesce(max(case when cs.ServIndex = 'AUDIT' then audit.staffcode end),'-') as 'Audit',
coalesce(max(case when cs.ServIndex = 'REVIEW' then review.staffcode end),'-') as 'Review',
coalesce(max(case when cs.ServIndex = 'COMP' then comp.staffcode end),'-') as 'Comp',
coalesce(max(case when cs.ServIndex = 'RandE' then rande.staffcode end),'-') as 'R&E',
coalesce(max(case when cs.ServIndex = 'ACCTG' then acctg.staffcode end),'-') as 'Acctg',
coalesce(max(case when cs.ServIndex = 'VMA' then vma.staffcode end),'-') as 'BV',
coalesce(max(case when cs.ServIndex = 'LIT' then lit.staffcode end),'-') as 'Lit',
coalesce(max(case when cs.ServIndex = 'FORENSIC' then lit.staffcode end),'-') as 'Forensics',
coalesce(max(case when cs.ServIndex = 'CONS' then lit.staffcode end),'-') as 'Consulting'
from tblEngagement e
inner join tblClientServices cs on cs.ContIndex = e.contindex
left outer join tblStaff ocr on ocr.StaffIndex = e.ClientPartner
left outer join tblStaff tax on tax.StaffIndex = cs.ServManager
left outer join tblStaff audit on audit.StaffIndex = cs.ServManager
left outer join tblStaff review on review.StaffIndex = cs.ServManager
left outer join tblStaff comp on comp.StaffIndex = cs.ServManager
left outer join tblStaff rande on rande.StaffIndex = cs.ServManager
left outer join tblStaff acctg on acctg.StaffIndex = cs.ServManager
left outer join tblStaff vma on vma.StaffIndex = cs.ServManager
left outer join tblStaff lit on lit.StaffIndex = cs.ServManager
left outer join tblStaff fore on fore.staffindex = cs.ServManager
where e.ContIndex < 900000 and cs.ServActPos = 1 and ClientStatus in ('ACTIVE','SUSPENDED') 
group by ocr.staffcode, clientcode, clientname
order by ClientCode

答案 1 :(得分:0)

此代码也有效,与我上面的Brett非常相似

 select ocr.staffcode as OCR, clientcode, clientname, 
 max(case when cs.ServIndex = 'TAXCOMP' then tax.StaffCode else '-' end) as 'Tax',
 max(case when cs.ServIndex = 'AUDIT' then audit.staffcode else '-' end) as 'Audit',
 max(case when cs.ServIndex = 'REVIEW' then review.staffcode else '-' end) as 'Review',
 max(case when cs.ServIndex = 'COMP' then comp.staffcode else '-' end) as 'Comp',
 max(case when cs.ServIndex = 'RandE' then rande.staffcode else '-' end) as 'R&E',
 max(case when cs.ServIndex = 'ACCTG' then acctg.staffcode else '-' end) as 'Acctg',
 max(case when cs.ServIndex = 'VMA' then vma.staffcode else '-' end) as 'BV',
 max(case when cs.ServIndex = 'LIT' then lit.staffcode else '-' end) as 'Lit',
 max(case when cs.ServIndex = 'FORENSIC' then lit.staffcode else '-' end) as 'Forensics',
 max(case when cs.ServIndex = 'CONS' then lit.staffcode else '-' end) as 'Consulting'
 from tblEngagement e
 inner join tblClientServices cs on cs.ContIndex = e.contindex
 left outer join tblStaff ocr on ocr.StaffIndex = e.ClientPartner
 left outer join tblStaff tax on tax.StaffIndex = cs.ServManager
 left outer join tblStaff audit on audit.StaffIndex = cs.ServManager
 left outer join tblStaff review on review.StaffIndex = cs.ServManager
 left outer join tblStaff comp on comp.StaffIndex = cs.ServManager
 left outer join tblStaff rande on rande.StaffIndex = cs.ServManager
 left outer join tblStaff acctg on acctg.StaffIndex = cs.ServManager
 left outer join tblStaff vma on vma.StaffIndex = cs.ServManager
 left outer join tblStaff lit on lit.StaffIndex = cs.ServManager
 left outer join tblStaff fore on fore.staffindex = cs.ServManager
 where e.ContIndex < 900000 and cs.ServActPos = 1 and ClientStatus in ('ACTIVE','SUSPENDED') 
 group by ocr.staffcode, clientcode, clientname
 order by ClientCode

感谢Brett