不好意思,上網詢問SQL語法,小妹功力不好.一直無法走出這個迴圈,只好上網求助,請各位前輩指導一下,謝謝
圖片裡有語法出來的內容及想要作的內容
語法如下:
select distinct dirdept.Code 上級部門編號,dirdept.Name 上級部門名稱,
',,,'+isnull(stuff((
select ',,,' +item.Name+'計算值'+replace(CONVERT(nvarchar(20),cast(sum(detail.ItemValue) as money),1),'.00','')
from SalaryResult result,SalaryResultDetail detail,SalaryItem item,Employee emp,SalaryMonth , Department dept
where result.SalaryResultId = detail.SalaryResultId
and result.EmployeeId = emp.EmployeeId
and result.DepartmentId = Department.DepartmentId
and detail.SalaryItemId = item.SalaryItemId
and result.SalaryMonthId = SalaryMonth.SalaryMonthId
and Department.ParentId = dept.DepartmentId
and (SalaryMonth.SalaryYear = '2018') AND (SalaryMonth.Month = '8')
AND (result.PayTimeNo = '2')
and Department.code LIKE 'F%'
AND item.name in ('底薪','伙食津貼')
and item.IsSalary = 1
and detail.ItemValue <> 0
group by item.ItemID,item.Name,dept.code
order by item.ItemID for xml path('')),1,3,''),'') +
(
select
isnull(case when SUM(result.LabourEmpFee) <>0 then ',,,勞保費計算值'+replace(CONVERT(nvarchar(20),cast(sum(result.LabourEmpFee) as money),1),'.00','') end,'') +
isnull(case when SUM(result.HealthFee) <>0 then ',,,健保費計算值'+replace(CONVERT(nvarchar(20),cast(sum(result.HealthFee) as money),1),'.00','') end,'')
from SalaryResult result
left join Employee emp on result.EmployeeId = emp.EmployeeId
left join SalaryMonth on result.SalaryMonthId = SalaryMonth.SalaryMonthId
left join Department dept1 on Department.ParentId = dept1.DepartmentId
where result.DepartmentId = Department.DepartmentId
and (SalaryMonth.SalaryYear = '2018') AND (SalaryMonth.Month = '8')
AND (result.PayTimeNo = '2')
and Department.code LIKE 'F%'
group by dept1.code
) 部門計算項 ,
'應發合計' 應發合計名,'應扣合計' 應扣合計名,'實發' 實發合計名,dirdept.name 上級部門
from SalaryResult
left join Employee on SalaryResult.EmployeeId = Employee.EmployeeId
left join Department on SalaryResult.DepartmentId = Department.DepartmentId
left join Corporation on Department.CorporationId = Corporation.CorporationId
left join SalaryMonth on SalaryResult.SalaryMonthId = SalaryMonth.SalaryMonthId
left join Department dirdept on Department.ParentId = dirdept.DepartmentId
where (SalaryMonth.SalaryYear = '2018') AND (SalaryMonth.Month = '8') AND (SalaryResult.PayTimeNo = '2') and Department.code LIKE 'F%'
猜的 :
group by item.ItemID,item.Name,dept.code
改成
group by item.ItemID,item.Name,Department.code
group by dept1.code
改成
group by Department.code