( select
SalaryResultId,
[ItemName] = stuff((
select
',' + isnull((SELECT CodeInfo.ScName FROM CodeInfo WHERE CodeInfo.InfoCode = [ItemName] AND CodeInfo.KindCode ='ENG' ),'找不到對應項目')
from (
select
SalaryResultDetail.SalaryResultId,
SalaryResultDetail.ItemId,
SalaryResultDetail.ItemName,
SalaryResultDetail.ItemValue
from SalaryResultDetail
left join SalaryItem on SalaryItem.SalaryItemId = SalaryResultDetail.SalaryItemId
where SalaryItem.IsSalary=1 and SalaryResultDetail.ItemValue<>0 and SalaryItem.ItemCat=0 and SalaryItem.SalaryItemTaxTypeId='SalaryItemTaxType_003')WTA
where WTA.SalaryResultId = WTAI.SalaryResultId
ORDER by WTA.ItemId for xml path('')) , 1 , 1 , ''),
[ItemValueSum] = SUM(WTAI.ItemValue),
[ItemValue] = stuff((
select
',' + cast([ItemValue] as varchar(50))
from (
select
SalaryResultDetail.SalaryResultId,
SalaryResultDetail.ItemId,
SalaryResultDetail.ItemName,
SalaryResultDetail.ItemValue
from SalaryResultDetail
left join SalaryItem on SalaryItem.SalaryItemId=SalaryResultDetail.SalaryItemId
where SalaryItem.IsSalary = 1 and SalaryResultDetail.ItemValue <> 0 and SalaryItem.ItemCat = 0 and SalaryItem.SalaryItemTaxTypeId ='SalaryItemTaxType_003')WTAV
where WTAV.SalaryResultId = WTAI.SalaryResultId
order by WTAV.ItemId for xml path('')) , 1 , 1 , '')
from (
select
detail.SalaryResultId,
detail.ItemId,
detail.ItemName,
detail.ItemValue
from SalaryResultDetail detail
left join SalaryItem on SalaryItem.SalaryItemId=detail.SalaryItemId
where SalaryItem.IsSalary=1 and detail.ItemValue<>0 and SalaryItem.ItemCat=0 and SalaryItem.SalaryItemTaxTypeId='SalaryItemTaxType_003') WTAI
group by SalaryResultId
)