|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
回复 17楼 wuxiang_123 的帖子--再次请教
前几天,我试过将下面这段进行转置,可怎么弄都不行,今天还是不行,不转置语句没问题,一转置就不行,郁闷啊!
select year(发货日期) as 年份,类型,sum(数量) as [数量] from [发货清单$] group by year(发货日期),类型 union all select 年份&'年累计' as 年份累计,类型,(select sum(数量1) from (select year(发货日期) as 年份,类型,sum(数量) as 数量1 from [发货清单$] GROUP BY 类型,year(发货日期)) a1 where a1.类型=a2.类型 and a1.年份<=a2.年份) from (select distinct year(发货日期) as 年份,类型 from [发货清单$] ) a2
我是这样写的:
transform sum(数量) select 年份 from
(select year(发货日期) as 年份,类型,sum(数量) as [数量] from [发货清单$] group by year(发货日期),类型
union all
select a2.年份&'年累计' as 年份累计,a2.类型,(select sum(数量1) from (select year(发货日期) as 年份,类型,sum(数量) as 数量1 from [发货清单$] GROUP BY 类型,year(发货日期)) a1 where a1.类型=a2.类型 and a1.年份<=a2.年份) from (select distinct year(发货日期) as 年份,类型 from [发货清单$] ) a2 group by a2.年份,a2.类型)
group by 年份 pivot 类型
如果改为如下就可以,真的不知为什么呢!
transform sum(数量汇总) select 年份 from
(select year(发货日期) as 年份,类型,sum(数量) as 数量汇总 from [发货清单$] group by year(发货日期),类型
union all
select a2.年份&'年累计' as 年份,a2.类型,sum(数量1) from
(select year(发货日期) as 年份,类型,sum(数量) as 数量1 from [发货清单$] GROUP BY 类型,year(发货日期)) a1,
(select distinct year(发货日期) as 年份,类型 from [发货清单$] ) a2 where a1.类型=a2.类型 and a1.年份<=a2.年份
group by a2.年份,a2.类型)
group by 年份 pivot 类型 |
|