0

我在两个不同的枢轴上得到计数值和总和。请指导我如何在单个查询中实现这两个功能。

计数枢轴:

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

总和枢轴:

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 

我想要这个图像的结果。请帮帮我。 在此处输入图像描述

4

1 回答 1

0

通过以下查询实现了这一点...

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'
) m
PIVOT (
COUNT(CurrentBalance)
FOR AgeBucket IN ([0-30],[31-60],[61-90],[91-120],[121-210],[>210])
) n
) m1
join 
(
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 
) n1

on m1.ClaimValue = n1.ClaimValue
order by m1.ClaimValue
于 2013-11-11T13:37:25.280 回答