你好,我被困在这里一个星期...... 说我在查询中有这个结果..
Branch Pay1 Pay2 Pay3 Pay4 Branch1 100 100 100 100 Branch1 150 150 150 150 Branch2 200 200 200 200 Branch3 200 200 200 200
我想做这样的事情
Branch Pay1 Pay2 Pay3 Pay4 Branch1 250 250 250 250 Branch2 200 200 200 200 Branch3 200 200 200 200
最后的结果是这样的
Item Branch1 Branch2 Branch3 pay1 250 200 200 pay2 250 200 200 pay3 250 200 200 pay4 250 200 200
希望你能帮助我做这件事......非常感谢..
这是我的查询...第一个结果
Select distinct
--pr_employees.Fullname as Name
--,
PR_Employees.BranchID,PR_payroll.BasicPay as [BasicPay]
,PR_Empearnings.EarningAmt
,PR_Earnings.Description
,pr_payroll.Overtime
,pr_payroll.Period
,Pr_payroll.SundayOT as [Sunday OT]
,Pr_Payroll.PaidHol as [Paid Hol]
,pr_payroll.ThirteenthMonthPay as [Thirteen MO]
,pr_payroll.Grosspay as [Gross Amount]
,pr_payroll.WithHoldingTax as [WithTax]
,pr_payroll.SSSPremium as [SSS Cont]
,pr_payroll.SSSLoan as [SSS Loan]
,pr_payroll.PagibigPremium as [Pagibig Cont]
,pr_payroll.PagibigLoan as [Pagibig Loan]
,pr_payroll.NHIPPremium as Medicare
,pr_payroll.TotalDeductions as [Total Ded]
,pr_Payroll.netpay as [Net with OD]
,pr_payroll.netnoOd as [Net no OD]
,prchargesAdvances.Credit
,prchargesadvancesTypes.ChargesTypeName
from pr_employees
left join pr_payroll on PR_Employees.EmpID=PR_Payroll.EmpID
left join PR_EmpEarnings on PR_Payroll.EmpID=PR_EmpEarnings.EmpID
left join PR_Earnings on PR_EmpEarnings.EarningId=pr_earnings.earningid
left join PR_Overtime on PR_Overtime.EmpID=PR_Payroll.EmpID
left join PRChargesAdvances on PRChargesAdvances.transactiondate=pr_payroll.period
and prchargesadvances.empid=pr_payroll.empid
left join PRChargesAdvancesTypes on PRChargesAdvances.ChargeTypeID=PRChargesAdvancesTypes.ChargesTypeID
where PR_Payroll.Period='8/31/2013'
答案 0 :(得分:0)
这不仅仅是一个模板,而是一个解决方案,但我会解释一下。
在下面的with子句中,您将按分支建立聚合。如果您将其作为自己的查询独立运行(重命名并添加所有付费列),这将为您提供所需输出的第一步(您帖子中的第一个所需结果集)。
然后你运行几个由工会连接的查询,你在其中指定你要汇总的薪水栏的文字(你必须把它作为文字输入,这就是为什么我用'literal'这个词来每次都说明。然后总结一下,每个pay字段一行,每个分支列(通过case语句),with子句中查询的聚合。我使用case语句之和的唯一原因是这样你就可以在每个薪资领域的同一行获得所有#s。
您有超过4个付费列(如您所知),因此您必须在完成之前添加几个联盟(并将所有付费列和分支ID重命名为它们)。
with sub as
(select branch,
sum(pay1) as sum_py1 sum(pay2) as sum_py2 sum(pay3) as sum_py3 sum(pay4) as sum_py4
from bunchoftbls
where PR_Payroll.Period = '8/31/2013'
group by branch)
select 'Pay1 literal'
, sum(case when branch = 'Branch1' then sum_py1 else 0 end) as branch1
, sum(case when branch = 'Branch2' then sum_py1 else 0 end) as branch2
, sum(case when branch = 'Branch3' then sum_py1 else 0 end) as branch3
, sum(case when branch = 'Branch4' then sum_py1 else 0 end) as branch4
, sum(case when branch = 'Branch5' then sum_py1 else 0 end) as branch5
, sum(case when branch = 'Branch6' then sum_py1 else 0 end) as branch6
from sub
union all
select 'Pay2 literal'
, sum(case when branch = 'Branch1' then sum_py2 else 0 end) as branch1
, sum(case when branch = 'Branch2' then sum_py2 else 0 end) as branch2
, sum(case when branch = 'Branch3' then sum_py2 else 0 end) as branch3
, sum(case when branch = 'Branch4' then sum_py2 else 0 end) as branch4
, sum(case when branch = 'Branch5' then sum_py2 else 0 end) as branch5
, sum(case when branch = 'Branch6' then sum_py2 else 0 end) as branch6
from sub
union all
select 'Pay3 literal'
, sum(case when branch = 'Branch1' then sum_py3 else 0 end) as branch1
, sum(case when branch = 'Branch2' then sum_py3 else 0 end) as branch2
, sum(case when branch = 'Branch3' then sum_py3 else 0 end) as branch3
, sum(case when branch = 'Branch4' then sum_py3 else 0 end) as branch4
, sum(case when branch = 'Branch5' then sum_py3 else 0 end) as branch5
, sum(case when branch = 'Branch6' then sum_py3 else 0 end) as branch6
from sub
union all
select 'Pay4 literal'
, sum(case when branch = 'Branch1' then sum_py4 else 0 end) as branch1
, sum(case when branch = 'Branch2' then sum_py4 else 0 end) as branch2
, sum(case when branch = 'Branch3' then sum_py4 else 0 end) as branch3
, sum(case when branch = 'Branch4' then sum_py4 else 0 end) as branch4
, sum(case when branch = 'Branch5' then sum_py4 else 0 end) as branch5
, sum(case when branch = 'Branch6' then sum_py4 else 0 end) as branch6
from sub