这样的SQL语句如何写呀

[复制链接]
查看11 | 回复4 | 2007-10-20 08:38:44 | 显示全部楼层 |阅读模式
发货单客户编码客户名称存货编码,存货名称 发货数量,发货金额
2006001 001
连锁一店
00001 分割品1000 50000
2006001 001
连锁一店
00002 白条 500 50000
。。。。。。。
2006002 002
连锁二店
00001 分割品500 25000
2006002 002
连锁二店
00002 白条 1500 150000

得到的汇总报表
客户编码(ccuscode) 客户名称(ccusname) 白条数量,白条金额 ,分割品数量,分割品金额, 发货数量合计,发货金额合计
001
连锁一店 500 50000 100050000 1500 100000

001
连锁二店 1500 150000500 250000
2000 300000
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
select ccuscode,max(ccusname),(case inventorycode='00002' then sum(quantity)esle 0 end )as 白条数量,(case inventorycode='00002' then sum(sum)else 0 )as 白条金额,(case inventorycode='00001' then sum(quantity)else 0)as 分割品数量,(case inventorycode='00001' then sum(sum)else 0end )as 分割品金额 group by ccuscode,inventorycode

可是不能在一行上呀,
请高手指教
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
答案见:blog.csdn.net/cosio
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
--建表
create table Order_Customer
(
Sorder varchar(7),
Cuscode varchar(3),
Cusname varchar(10),
Stocode varchar(5),
Stoname varchar(10),
SStoquantity int,
SStomoney int
)
insert Order_Customer
select '2006001','001','连锁一店','00001','分割品',1000,50000
union all
select '2006001','001','连锁一店','00002','白条',500,50000
union all
select '2006002','002','连锁二店','00001','分割品',500,250000
union all
select '2006002','002','连锁二店','00002','白条',1500,150000
union all
select '2006002','002','连锁二店','00001','分割品',500,250000
union all
select '2006002','002','连锁二店','00002','白条',1500,150000
--检索所需
select '客户编码'=Cuscode , '客户名称'=Cusname
,'白条数量'= sum(case Stocodewhen 00002 then SStoquantity end)
,'白条金额'=sum(case Stocodewhen 00002 then SStomoney end)
,'分割品数量'=sum(case Stocodewhen 00001 then SStoquantity end)
,'分割品金额'=sum(case Stocodewhen 00001 then SStomoney end)
,'发货数量合计'=sum(case Stocodewhen 00002 then SStoquantity end)+sum(case Stocodewhen 00001 then SStoquantity end)
,'发货金额合计'=sum(case Stocodewhen 00002 then SStomoney end)+sum(case Stocodewhen 00001 then SStomoney end)
from Order_Customer
group by Sorder,Cuscode,Cusname

drop table Order_Customer
回复

使用道具 举报

千问 | 2007-10-20 08:38:44 | 显示全部楼层
谢谢四楼的处理,我试一下。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行