pivot列行转换,自动计算分组,解决groupby问题


select top 20 * from
(
select PromotionRateAmount,(cast(year(ReserveDate) as varchar(4))+cast(month(ReserveDate) as varchar(2))) as MonthSum,OrderId
from [PAAmountAndDay] WITH(NOLOCK)
where orderid in (select distinct orderid from [FlMortgageDetailGroupByMortgageContractNoAndOrderID])
) as a
pivot(sum(PromotionRateAmount) for MonthSum in ([20151],[20152],[20153],[20154])) as b

原文地址:https://www.cnblogs.com/liuchuanxu/p/4707510.html