本文介绍了如何在sql server 2012中加入两个枢轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我得到了两个不同枢轴的计数值和总和。请指导我如何在单个查询中实现这两个功能。 Count Pivot: SELECT * FROM ( SELECT ClaimValue,CurrentBalance,AgeBucket FROM AR_Claim_Master WHERE ClientName = ' NXT' )m PIVOT( COUNT(CurrentBalance) FOR AgeBucket IN ([0-30],[31- 60],[61-90],[91-120],[121-210],[> 210]))n Sum Pivot: SELECT * FROM ( SELECT ClaimValue,CurrentBalance,AgeBucket FROM AR_Claim_Master WHERE ClientName = ' NXT' )m PIVOT( SUM(CurrentBalance) FOR AgeBucket IN ([0-30],[31-60],[61-90],[91-120],[121-210],[> 210]))n 解决方案 通过以下查询达到此目的... 选择 m1.ClaimValue, m1。[0-30] [0-30c],n1。[0-30] [ 0-30v], m1。[31-60] [31-60c],n1。[31-60] [31-60v], m1。[61-90] [61-90c ],n1。[61-90] [61-90v], m1。[91-120] [91-120c],n1。[91-120] [91-120v], m1 。[121-210] [121-210c],n1。[121-210] [121-210v], m1。[> 210] [> 210c],n1。[> 210] [ > 210v] 来自( SELECT * FROM ( SEL ECT ClaimValue,CurrentBalance,AgeBucket FROM AR_Claim_Master WHERE ClientName = ' NXT' )m PIVOT( COUNT( CurrentBalance) FOR AgeBucket IN ([0-30],[31-60], [61-90],[91-120],[121-210],[> 210]))n )m1 加入 ( SELECT * FROM ( SELECT ClaimValue,CurrentBalance,AgeBucket FROM AR_Claim_Master WHERE ClientName = ' NXT' )m PIVOT( SUM(CurrentBalance) FOR 年龄Bucket IN ([0-30],[31-60],[61-90],[91-120],[121-210],[> 210]))n )n1 on m1.ClaimValue = n1.ClaimValue order by m1.ClaimValue I m getting the count value and sum in two different pivots. Please guide me how to achieve the both function in a single query.Count Pivot:SELECT * FROM (SELECT ClaimValue, CurrentBalance, AgeBucketFROM AR_Claim_MasterWHERE ClientName = 'NXT') mPIVOT (COUNT(CurrentBalance)FOR AgeBucket IN ([0-30],[31-60],[61-90],[91-120],[121-210],[>210])) nSum Pivot:SELECT * FROM (SELECT ClaimValue, CurrentBalance, AgeBucketFROM AR_Claim_MasterWHERE ClientName = 'NXT') mPIVOT (SUM(CurrentBalance)FOR AgeBucket IN ([0-30],[31-60],[61-90],[91-120],[121-210],[>210])) n 解决方案 Achieved this thru the below query...select m1.ClaimValue, m1.[0-30] [0-30c], n1.[0-30][0-30v], m1.[31-60] [31-60c], n1.[31-60] [31-60v], m1.[61-90] [61-90c], n1.[61-90] [61-90v],m1.[91-120] [91-120c], n1.[91-120] [91-120v],m1.[121-210] [121-210c], n1.[121-210] [121-210v], m1.[>210] [>210c], n1.[>210] [>210v]from (SELECT *FROM ( SELECT ClaimValue, CurrentBalance, AgeBucket FROM AR_Claim_Master WHERE ClientName = 'NXT') mPIVOT ( COUNT(CurrentBalance) FOR AgeBucket IN ([0-30],[31-60],[61-90],[91-120],[121-210],[>210])) n) m1join (SELECT *FROM ( SELECT ClaimValue, CurrentBalance, AgeBucket FROM AR_Claim_Master WHERE ClientName = 'NXT') mPIVOT ( SUM(CurrentBalance) FOR AgeBucket IN ([0-30],[31-60],[61-90],[91-120],[121-210],[>210])) n ) n1on m1.ClaimValue = n1.ClaimValueorder by m1.ClaimValue 这篇关于如何在sql server 2012中加入两个枢轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 00:51