请教sql高手一条类似excel分类汇总似的语句,请高人赐教,越详细越数据表(60分)

  • 主题发起人 主题发起人 richard0922
  • 开始时间 开始时间
R

richard0922

Unregistered / Unconfirmed
GUEST, unregistred user!
数据表 <br>方式 日期 商品 仓库 数量 单价 金额<br>出库 2007-9-29 钢笔 东库 -30 1.8 -54<br>入库 2007-4-4 钢笔 东库 50 1.8 90<br>入库 2007-4-4 书包 西库 50 61.8368 3091.84<br>出库 2007-5-5 书包 西库 -50 61.8368 -3091.84<br> <br> <br>求如何得到下面数据的SQL写法 <br><br>方式 日期 商品 仓库 数量 单价 金额<br>出库 2007-9-29 钢笔 东库 -30 1.8 -54<br>入库 2007-4-4 钢笔 东库 50 1.8 90<br>&nbsp; 小计 20 36<br>入库 2007-4-4 书包 西库 50 61.8368 3091.84<br>出库 2007-5-5 书包 西库 -50 61.8368 -3091.84<br>&nbsp; 小计 0 0<br>合计 20 36
 
说明:SQL的写法我只想用GROUP &nbsp; BY 商品,仓库分组,但又要在查询里显示日期和方式,在小计和合计行只显示数量和金额的合,想要的结果如下格式,请仔细看好。 <br>方式 &nbsp; &nbsp; &nbsp; &nbsp; 日期 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 商品 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 仓库 &nbsp; &nbsp; &nbsp; &nbsp; 数量 &nbsp; &nbsp; &nbsp; &nbsp; 单价 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 金额 &nbsp; <br>—————————————————————————————————— <br>出库 &nbsp; &nbsp; &nbsp; 2007-9-29 &nbsp; &nbsp; 钢笔 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 东库 &nbsp; &nbsp; &nbsp; &nbsp; -30 &nbsp; &nbsp; &nbsp; &nbsp; 1.8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -54 &nbsp; <br>—————————————————————————————————— <br>入库 &nbsp; &nbsp; &nbsp; 2007-4-4 &nbsp; &nbsp; &nbsp; 钢笔 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 东库 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 50 &nbsp; &nbsp; &nbsp; &nbsp; 1.8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 90 &nbsp; <br>—————————————————————————————————— <br>&nbsp; 小计 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 20 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 36 &nbsp; <br>—————————————————————————————————— <br>入库 &nbsp; &nbsp; &nbsp; 2007-4-4 &nbsp; &nbsp; &nbsp; 书包 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 西库 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 50 &nbsp; &nbsp; &nbsp; &nbsp; 61.8368 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3091.84 &nbsp; <br>—————————————————————————————————— <br>出库 &nbsp; &nbsp; &nbsp; 2007-5-5 &nbsp; &nbsp; &nbsp; 书包 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 西库 &nbsp; &nbsp; &nbsp; &nbsp; -50 &nbsp; &nbsp; &nbsp; &nbsp; 61.8368 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -3091.84 &nbsp; <br>—————————————————————————————————— <br>&nbsp; 小计 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; <br>—————————————————————————————————— <br>合计 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 20 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 36 &nbsp; <br>——————————————————————————————————
 
看看1)、 with rollup <br>2)、computer<br>的帮助吧。。
 
好像必须使用某控件吧,否则不好实现.
 
select * from (<br>select 1 xslb,方式,日期,商品,仓库,数量,单价,金额 from 表<br>union all<br>select 2 xslb,'小计',null,商品,仓库,sum(数量),null,sum(金额) from 表 group by 商品,仓库<br>)aa<br>order by 商品,仓库,xslb
 
SQL Server 用Grouping子句,其他数据库没有接触过,看看 SQL Server 帮助吧,肯定能实现。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
851
import
I
后退
顶部