怎样不用游标实现这样的统计

lqe_ada 2003-10-23 03:23:50
A B C 出货 进货 style

a b c 10 1
a b c 1 -1
a b d 10 1
a b e 1 1

A,B,C 组成主键,现在要求如下的结果(不用游标)。。。。

A B C 出货 进货 实际出货

a b c 10 1 9
a b d 10 10
a b e 1 -1
...全文
42 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzh1215 2003-10-24
  • 打赏
  • 举报
回复
select A,B,C,sum(isnull(出货,0)) 出货,sum(isnull(进货,0)) 进货,
sum(isnull(出货,0) - isnull(进货,0)) 实际出货 from table1
group by A,B,C
txlicenhe 2003-10-24
  • 打赏
  • 举报
回复
select A,B,C,sum(出货) as 出货,sum(IsNull(进货,0)) as 进货,
sum(IsNull(出货,0)*style)+sum(IsNull(进货,0)*style) as 实际进货
from 表 group by A,B,C
Frewin 2003-10-24
  • 打赏
  • 举报
回复
select A,B,C,sum(出货) as 出货,sum(进货) as 进货,
sum(出货*style)+sum(进货*style) as 实际进货
from 表 group by A,B,C
orcale 2003-10-23
  • 打赏
  • 举报
回复
select a,b,c,sum(出貨) as 出貨,sum(進貨) as 進貨,(sum(出貨)-sum(進貨))
as 實際出貨 from table group by a,b,c
Dennis618 2003-10-23
  • 打赏
  • 举报
回复
select a,b,c,sum(出货) as 出货,sum(进货) as 进货,isnull(sum(出货),0)-isnull(sum(进货),0) as 实际出货 from table3 group by a,b,c
welyngj 2003-10-23
  • 打赏
  • 举报
回复
create table aa
( a char,
b char,
c char,
ex int,
im int,
style int)
insert into aa select 'a','b','c',10,null,1
insert into aa select 'a','b','c',null,1,-1
insert into aa select 'a','b','d',10,null,1
insert into aa select 'a','b','e',null,1,-1


select a,b,c,max(ex) as ex ,max(im) im ,sum(isnull(ex,0))-sum(isnull(im,0)) from aa group by a,b,c
yujohny 2003-10-23
  • 打赏
  • 举报
回复
select A,B,C,sum(出货) as 出货,sum(进货) as 进货,
sum(出货*style)+sum(进货*style) as 实际进货
from 表 group by A,B,C
samuelpan 2003-10-23
  • 打赏
  • 举报
回复
select A,B,C,
Case When Style=1 Then Sum(IsNull(出货,0)) End As 出货,
Case When Style=-1 Then Sum(IsNull(进货,0)) End As 进货,
Sum(IsNull(出货,0))-Sum(IsNull(进货,0)) As 实际出货
from table
group by a,b,c
arrow_gx 2003-10-23
  • 打赏
  • 举报
回复
select A,B,C,sum(出货),sum(进货),sum(出货)-sum(进货) as 实际出货 from yourtable

group by a,b,c
愉快的登山者 2003-10-23
  • 打赏
  • 举报
回复
select A,B,C,
sum(isnull(出货,0)) 出货,
sum(isnull(进货,0)) 进货,
sum(isnull(出货,0) - isnull(进货,0)) 实际出货 from table1
group by A,B,C
lqe_ada 2003-10-23
  • 打赏
  • 举报
回复
对不起,写错了。正确的应该如下:
A B C 出货 进货 style

a b c 10 1
a b c 1 -1
a b d 10 1
a b e 1 -1

A,B,C 组成主键,现在要求如下的结果。。。。

A B C 出货 进货 实际出货

a b c 10 1 9
a b d 10 10
a b e 1 -1

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧