我需要一些幫助來開發將組合相同客戶端的行的查詢。基本上我有一個客戶端,有一個稅務經理,一個審計經理,一個客戶經理等。合併行SQL查詢特定
到目前爲止我已經使用的代碼,我能夠拉起信息,但它吐出了幾行同一客戶端對於每種類型的經理
圖片上的示例(帶有代碼和結果)。我在下面添加了一個鏈接,它不會讓我直接添加圖片。
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,客戶名稱的一排顯示的稅務經理,審計經理,審查經理等全部在一行......如果沒有經理爲其中一個顯示「 - 」,就像我在我的例子中那樣。我一直在讀幾個地方,試圖做自己,但沒有運氣。任何人都可以幫助我指引正確的方向?
是數據庫的MySQL或SQL Server?請相應標記問題。 –
查找'SELECT DISTINCT'和'GROUP BY'。 – miyasudokoro
我正在運行Microsoft SQL Server管理工作室(Microsoft SQL Server 2008 R2)是什麼意思? – andres