我在没有透视的查询中有这个结果,看起来像这样
Branch Payroll Items AAA 0 BasicPay AAA 0 Overtime AAA 0 Paid Hol AAA 0 Sunday OT AAA 0 Thirteen MO AAA 0 Gross Amount AAA 0 WithTax AAA 0 SSS Cont AAA 0 SSS Loan AAA 0 Pagibig Cont AAA 0 Pagibig Loan AAA 0 Medicare AAA 0 Total Ded AAA 0 Net with OD AAA 0 Net no OD AAA 0 CASH ADVANCE AAA 0 MEAL AAA 0 Cola AAA 0 Incentives
但是在旋转表之后我的项目结果变得混乱..这就是结果..
Items AAA BasicPay 0 CASH ADVANCE 0 Cola 0 Gross Amount 0 Incentives 0 MEAL 0 Medicare 0 Net no OD 0 Net with OD 0 Overtime 0 Pagibig Cont 0 Pagibig Loan 0 Paid Hol 0 SSS Cont 0 SSS Loan 0 Sunday OT 0 Thirteen MO 0 Total Ded 0 WithTax 0
为什么会这样?我希望在旋转后保留我的项目字段的顺序。我可以这样做吗?请多多帮助... 这是我的查询
select * from( Select unpivots.Branch,items,items2 from( Select BranchId as testid,Coalesce([BasicPay],0)[BasicPay],Coalesce(Overtime,0)overtime,coalesce([Paid Hol],0)[Paid Hol],coalesce([Sunday OT],0)[Sunday OT],Coalesce([Thirteen MO],0)[Thirteen MO], coalesce([Gross Amount],0)[Gross Amount],coalesce([WithTax],0)[WithTax],coalesce([SSS Cont],0)[SSS Cont],coalesce([SSS Loan],0)[SSS Loan],coalesce([Pagibig Cont],0)[Pagibig Cont],coalesce([Pagibig Loan],0)[Pagibig Loan] ,coalesce(Medicare,0)[Medicare],Coalesce([Total Ded],0)[Total Ded],coalesce([Net with OD],0)[Net with OD],Coalesce([Net no OD],0)[Net no OD],coalesce([CASH ADVANCE],0)[CASH ADVANCE],coalesce([MEAL],0)[MEAL],Coalesce([Cola],0)[Cola],coalesce([Incentives],0)[Incentives],Branch from( Select BranchId as testid,Coalesce([BasicPay],0)[BasicPay],Coalesce(Overtime,0)overtime,coalesce([Paid Hol],0)[Paid Hol],coalesce([Sunday OT],0)[Sunday OT],Coalesce([Thirteen MO],0)[Thirteen MO], coalesce([Gross Amount],0)[Gross Amount],coalesce([WithTax],0)[WithTax],coalesce([SSS Cont],0)[SSS Cont],coalesce([SSS Loan],0)[SSS Loan],coalesce([Pagibig Cont],0)[Pagibig Cont],coalesce([Pagibig Loan],0)[Pagibig Loan] ,coalesce(Medicare,0)[Medicare],Coalesce([Total Ded],0)[Total Ded],coalesce([Net with OD],0)[Net with OD],Coalesce([Net no OD],0)[Net no OD],coalesce([CASH ADVANCE],0)[CASH ADVANCE],coalesce([MEAL],0)[MEAL],Coalesce([Cola],0)[Cola],coalesce([Incentives],0)[Incentives] from ( 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' ) as Source pivot ( MAX([EarningAmt]) FOR [Description] IN ([COLa],[Incentives]) ) as pvt pivot ( MAX([Credit]) FOR [ChargesTypeName] IN ([MEAL],[CASH ADVANCE]) ) as pvt2)AS Srck right join PR_Branch on Srck.testID=PR_Branch.BranchID)as src unpivot(items for items2 in ([BasicPay],Overtime,[Paid Hol],[Sunday OT],[Thirteen MO], [Gross Amount],[WithTax],[SSS Cont],[SSS Loan],[Pagibig Cont],[Pagibig Loan] ,Medicare,[Total Ded],[Net with OD],[Net no OD],[CASH ADVANCE],[MEAL],[Cola],[Incentives]))as unpivots )as latestsrc pivot (sum(items) for items2 in ([BasicPay],Overtime,[Paid Hol],[Sunday OT],[Thirteen MO], [Gross Amount],[WithTax],[SSS Cont],[SSS Loan],[Pagibig Cont],[Pagibig Loan] ,Medicare,[Total Ded],[Net with OD],[Net no OD],[CASH ADVANCE],[MEAL],[Cola],[Incentives])) as pvt unpivot(Payroll for Items in ([BasicPay],Overtime,[Paid Hol],[Sunday OT],[Thirteen MO], [Gross Amount],[WithTax],[SSS Cont],[SSS Loan],[Pagibig Cont],[Pagibig Loan] ,Medicare,[Total Ded],[Net with OD],[Net no OD],[CASH ADVANCE],[MEAL],[Cola],[Incentives]))as unpvt pivot (max(payroll) for Branch in ([aaa]))as pvt
答案 0 :(得分:1)
只需使用row_number() OVER (ORDER BY (SELECT 0))
作为此列的列和顺序,希望您将解决您的问题,下面给出了与您的数据相关的示例。
CREATE table #Temp (Branch varchar(255),Payroll int, Itemsvarchar(255))
INSERT INTO #Temp VALUES
('AAA',0,'BasicPay')
,('AAA',0,'Overtime')
,('AAA',0,'Paid Hol')
,('AAA',0,'Sunday OT')
,('AAA',0,'Thirteen MO')
,('AAA',0,'Gross Amount')
,('AAA',0,'WithTax')
,('AAA',0,'SSS Cont')
,('AAA',0,'SSS Loan')
,('AAA',0,'Pagibig Cont')
,('AAA',0,'Pagibig Loan')
,('AAA',0,'Medicare')
,('AAA',0,'Total Ded')
,('AAA',0,'Net with OD')
,('AAA',0,'Net no OD')
,('AAA',0,'CASH ADVANCE')
,('AAA',0,'MEAL')
,('AAA',0,'Cola')
,('AAA',0,'Incentives')
创建一个sortKey row_number() OVER (ORDER BY (SELECT 0))
,然后在枢轴中按该排序键排序。
select Items,[AAA] from
(
select Branch
,Payroll
,Items
,row_number() OVER (ORDER BY (SELECT 0)) sortKey
from #Temp ) ABC
PIVOT
(
MAX(Payroll)
FOR Branch IN ([AAA])
) as pvt2
order by sortKey
DROP TABLE #Temp
输出:
Items AAA
BasicPay 0
Overtime 0
Paid Hol 0
Sunday OT 0
Thirteen MO 0
Gross Amount 0
WithTax 0
SSS Cont 0
SSS Loan 0
Pagibig Cont 0
Pagibig Loan 0
Medicare 0
Total Ded 0
Net with OD 0
Net no OD 0
CASH ADVANCE 0
MEAL 0
Cola 0
Incentives 0