|
总算弄出来了。- transform sum(数量) select 年份 from
- (SELECT 类型,年份,SUM(数量) as 数量 FROM
- (
- select A2.类型,A2.年份&"累计" AS 年份,sum(A1.S) as 数量 from
- (select year(发货日期) as 年份,类型,sum(数量) as S from [发货清单$] GROUP BY 类型,year(发货日期)) a1,
- (select DISTINCT 类型,年份 from
- (select DISTINCT 类型 from [发货清单$])a,
- (select DISTINCT year(发货日期) as 年份 from [发货清单$])b)A2
- where a1.类型=a2.类型 and a1.年份<=a2.年份 GROUP BY A2.类型,A2.年份&"累计"
- union all
- select 类型,YEAR(发货日期),SUM(数量) FROM [发货清单$] GROUP BY 类型,year(发货日期)
- UNION ALL
- select DISTINCT 类型,年份,NULL from
- (select DISTINCT 类型 from [发货清单$])T,
- (select DISTINCT year(发货日期) as 年份 from [发货清单$])F
- ) GROUP BY 类型,年份 ORDER BY 类型,年份
- )GROUP BY 年份
- pivot 类型
复制代码- 部分语句多余,可以简化为:
- transform sum(数量) select 年份 from
- (
- select A2.类型,A2.年份&"累计" AS 年份,sum(A1.S) as 数量 from
- (select year(发货日期) as 年份,类型,sum(数量) as S from [发货清单$] GROUP BY 类型,year(发货日期)) a1,
- (select 类型,年份 from
- (select DISTINCT 类型 from [发货清单$])a,
- (select DISTINCT year(发货日期) as 年份 from [发货清单$])b)A2
- where a1.类型=a2.类型 and a1.年份<=a2.年份 GROUP BY A2.类型,A2.年份&"累计"
- union all
- select 类型,YEAR(发货日期),SUM(数量) FROM [发货清单$] GROUP BY 类型,year(发货日期)
- UNION ALL
- select 类型,年份,NULL from
- (select DISTINCT 类型 from [发货清单$])T,
- (select DISTINCT year(发货日期) as 年份 from [发货清单$])F
- )GROUP BY 年份
- pivot 类型
复制代码- 再减几个字符:
- transform sum(数量) select 年份 from
- (
- select A2.类型,A2.年份&"累计" AS 年份,A1.S as 数量 from
- (select year(发货日期) as 年份,类型,sum(数量) as S from [发货清单$] GROUP BY 类型,year(发货日期)) a1,
- (select 类型,年份 from
- (select DISTINCT 类型 from [发货清单$])a,
- (select DISTINCT year(发货日期) as 年份 from [发货清单$])b)A2
- where a1.类型=a2.类型 and a1.年份<=a2.年份
- union all
- select 类型,YEAR(发货日期),数量 FROM [发货清单$]
- UNION ALL
- select 类型,年份,NULL from
- (select DISTINCT 类型 from [发货清单$])T,
- (select DISTINCT year(发货日期) as 年份 from [发货清单$])F
- )GROUP BY 年份
- pivot 类型
复制代码
[ 本帖最后由 wuxiang_123 于 2010-8-30 17:06 编辑 ] |
|