2014-01-14 42 views
-1

我需要一些幫助來開發將組合相同客戶端的行的查詢。基本上我有一個客戶端,有一個稅務經理,一個審計經理,一個客戶經理等。合併行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,客戶名稱的一排顯示的稅務經理,審計經理,審查經理等全部在一行......如果沒有經理爲其中一個顯示「 - 」,就像我在我的例子中那樣。我一直在讀幾個地方,試圖做自己,但沒有運氣。任何人都可以幫助我指引正確的方向?

+2

是數據庫的MySQL或SQL Server?請相應標記問題。 –

+1

查找'SELECT DISTINCT'和'GROUP BY'。 – miyasudokoro

+0

我正在運行Microsoft SQL Server管理工作室(Microsoft SQL Server 2008 R2)是什麼意思? – andres

回答

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 
+0

謝謝您的解決方案,只是看到它,會試一試並回復您! – andres

+0

謝謝你的回答,它按需要工作,現在只需等待,看看是否足夠。此外,我今天寫的代碼也適用,如果沒有「合併」,您是否可以向我解釋爲什麼您使用了合併? – andres

+0

我使用了coalesce來輸入缺少值的''''。原因是因爲你在'case'的'else'分支中有'''',本質上也是這樣。 –

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 

感謝佈雷特