我有这样的数据
TYPE RANGE SALES
A 20180301-20180101 100
A 20171201-20171031 150
A 20170930-20170731 100
B 20180301-20180101 200
B 20171201- 20171031 200
我想要这样的输出
TYPE 20180301-20180101 20171201-20171031 20170930-20170731
A 100 150 100
我试过这个
SELECT
FROM
(
SELECT TYPE,MONTH_RANGE,SALES
FROM TABLE
)
pivot
(
SUM(SALES)
FOR (MONTH_RANGE) IN (SELECT DISTINCT MONTH_RANGE FROM TABLE)
)
并尝试了这个
pivot xml (SUM(SALES) for (month_range) in (SELECT DISTINCT MONTH_RANGE FROM TABLE))
我希望这个月的范围是动态的,而不是for month_range in ('20180301-20180101')