2014-02-21 124 views
0

你好我已經困在這裏了一個星期...... 說我有這樣的結果我的查詢..的Sql樞紐報表

 
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 

希望你能幫助我做this..thanks很多..

順便說一下,這是我的query..for第一個結果

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

你運行了什麼查詢來獲得該結果? (請粘貼sql) –

+0

我已經發布了我的查詢..但這僅僅是第一個結果 – user3312649

+0

存在多少個分支ID?如果必須在查詢中指定它們全部,它是可以接受的嗎? –

回答

0

這是一個多解決方案模板,但我會解釋。

在下面的with子句中,您正在按分支建立聚合。如果您將其作爲自己的查詢單獨運行(重命名並添加所有薪酬列),這將使您向期望的輸出(您的文章中第一個期望的結果集)邁出第一步。

然後,您運行幾個通過聯合連接的查詢,您在其中指定了要彙總的支付列的字面值(您必須將其作爲文字鍵入,這就是爲什麼我使用「literal」然後總結一下,每個支付字段和每個分支的列(通過一個case語句),來自在with子句中查詢的總和。我使用case語句總和的唯一原因這樣你就可以在同一行上爲每個支付字段獲得所有#

你有超過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 
+0

嗯,我認爲這會讓程序變慢。 – user3312649