实现类似于EXCEl 的分类汇总,汇总行在最下面,而且是多级汇总。
dbf表 类别C(10),编号C(10),数量n(10),金额n(10,2)
先按编号 对数量、金额汇总,汇总行显示在下方(原纪录要显示)。
再按类别汇总。
格式类如:
类别1 编号1 20 30.00
类别1 编号1 10 15.00
类别1 编号1小计 30 45.00
类别1 编号2 10 30.00
类别1 编号2小计 10 30.00
类别1小计 40 75.00
类别2 编号3 10 20.00
... ... .. ...
语句该怎么写呢?望高手们指教。
------解决方案--------------------------------------------------------
示例:
select 类别,编号,数量,sum(金额) from tt group by 类别,编号,数量
union
select 类别,编号,sum(数量),sum(金额) from tt group by 类别,编号
union
select 类别,'',sum(数量),sum(金额) from tt group by 类别
------解决方案--------------------------------------------------------
VFP6.0
- SQL code
Create Cursor T1 (类别 C(10),编号 C(10),数量 N(10),金额 N(10,2))Insert Into T1 Values ("类别1","编号1",20,30)Insert Into T1 Values ("类别1","编号1",10,15)Insert Into T1 Values ("类别1","编号2",10,30)Insert Into T1 Values ("类别2","编号3",10,20)Select *,类别 As LB,编号 As BH From T1 Union All; Select 类别,Alltrim(编号)+'小计' As 编号,Sum(数量) As 数量,Sum(金额) As 金额,类别 AS LB,编号 AS BH From T1 Group By LB,BH Union All; Select Alltrim(类别)+'小计' As 类别,'' As 编号,Sum(数量) As 数量,Sum(金额) As 金额,类别 AS LB,编号 AS BH From T1 Group By LB Union All; Select '类别总计' As 类别,'' AS 编号,Sum(数量) As 数量,Sum(金额) As 金额,'' AS LB,'' As BH From T1 Order By 1,2