请教一道题:查询中的运算

fqli1610 2010-10-21 01:59:26
如下表,

表a

仓库 存货 收发标示 数量
天河仓 A 收 100
天河仓 A 收 200
天河仓 B 收 10
白云仓 A 收 10
白云仓 B 收 80
天河仓 A 发 50
天河仓 B 收 20
白云仓 B 发 50
… … … …


想要查询的结果:

仓库 存货 现存数量
天河仓 A 250
天河仓 B 30
白云仓 A 10
白云仓 B 30
… … …


这个sql查询语句该如何写呢,谢谢大侠了,若有多种方法更好。。。
...全文
280 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
fqli1610 2010-10-21
  • 打赏
  • 举报
回复
此地真高手如云,问题解决了,谢谢各位,


结贴散发,
分数有限,
大家谅解,
fengyoujie 2010-10-21
  • 打赏
  • 举报
回复

if object_id('a') is not null drop table a
create table a(id int identity(1,1) primary key,仓库 varchar(50),存货 varchar(10),收发标示 varchar(2),数量 int);
insert into a
select '天河仓', 'A', '收', 100 union all
select '天河仓', 'A', '收', 200 union all
select '天河仓', 'B', '收', 10 union all
select '白云仓', 'A', '收', 10 union all
select '白云仓', 'B', '收', 80 union all
select '天河仓', 'A', '发', 50 union all
select '天河仓', 'B', '收', 20 union all
select '白云仓', 'B', '发', 50
----------建表插入数据是引用#13 xiaoxiao8372的,特此指出
---------------
--想要查询的结果:
--仓库 存货 现存数量
--天河仓 A 250
--天河仓 B 30
--白云仓 A 10
--白云仓 B 30

select 仓库,存货 ,sum(case 收发标示 when '收' then 数量 else -数量 end) 现存数量
from a
group by 仓库,存货
----------------结果
/*
白云仓 A 10
天河仓 A 250
白云仓 B 30
天河仓 B 30

(4 行受影响)
*/
lengjing126 2010-10-21
  • 打赏
  • 举报
回复
高手都挺多的,貌似都对。
七不语v 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 only_endure 的回复:]
SQL code
insert into # select 仓库,存货,数量=(case when 收发标志=“收” then 数量 else (-1)*数量)from tab
select 仓库,存货,sum(数量) from # group by 仓库,存货
[/Quote]
正解
baiying15 2010-10-21
  • 打赏
  • 举报
回复

select 仓库,存货,sum(case when 收发标志='收' then 数量 else (-1)*数量 end)as 数量 from tab
group by 仓库,存货
JJYY0088 2010-10-21
  • 打赏
  • 举报
回复
不好意思 ,上面查询没有考虑到 “收”,“发”
JJYY0088 2010-10-21
  • 打赏
  • 举报
回复
表数据:
names                                              types      counts
-------------------------------------------------- ---------- -----------
天河仓 A 200
天河仓 A 50
天河仓 B 30
天河仓 C 200
白云仓 A 50
白云仓 A 50
白云仓 C 100

-- names:仓库 types:型号 conts:数量
select names,types,sum(counts) from tb group by names,types order by names

names types
---------------- ---------- -----------
白云仓 A 100
白云仓 C 100
天河仓 A 250
天河仓 B 30
天河仓 C 200
hecker728 2010-10-21
  • 打赏
  • 举报
回复

if object_id('tempdb.dbo.#') is not null drop table #
create table #t(仓库 varchar(8), 存货 varchar(8),收发标志 varchar(8),数量 int)
insert into #t
select '天河仓', 'A','收', 100 union all
select '天河仓', 'A','收', 200 union all
select '天河仓', 'B','收', 10 union all
select '白云仓', 'A','收', 10 union all
select '白云仓', 'B','收', 80 union all
select '天河仓', 'A','发', 50 union all
select '天河仓', 'B','收', 20 union all
select '白云仓', 'B','发', 50


select 仓库,存货,sum(数量) 数量 from
(select 仓库,存货,数量=(case when 收发标志='收' then 数量 else (-1)*数量 end) from #t) as tab
group by 仓库,存货

drop table #t
/*--------------------------
仓库 存货 数量

白云仓 A 10
天河仓 A 250
白云仓 B 30
天河仓 B 30
-----------------------------*/

linjunf 2010-10-21
  • 打赏
  • 举报
回复
10fen~~~~~~~~~~~~~~~~~~~~
一品梅 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 bancxc 的回复:]
SQL code
select 仓库,存货,数量=sum(case when 收发标志='收' then 数量 else (-1)*数量 end)from tab
group by 仓库,存货
[/Quote]
我有个心愿,就是给我心爱的表姐散分。。。
xiaoxiao8372 2010-10-21
  • 打赏
  • 举报
回复

create table stock(
id int identity(1,1) primary key,
depot varchar(10) not null ,
stockname varchar(10) not null,
flag varchar(2) not null,
num int not null
)
insert into stock(depot,stockname,flag,num)
select '天河仓', 'A', '收', 100 union all
select '天河仓', 'A', '收', 200 union all
select '天河仓', 'B', '收', 10 union all
select '白云仓', 'A', '收', 10 union all
select '白云仓', 'B', '收', 80 union all
select '天河仓', 'A', '发', 50 union all
select '天河仓', 'B', '收', 20 union all
select '白云仓', 'B', '发', 50


select depot,stockname,
sum(case when flag='收' then num else -num end) realnum
from stock
group by depot,stockname
order by depot desc

abuying 2010-10-21
  • 打赏
  • 举报
回复
select 仓库,存货,数量=sum(case when 收发标志='收' then 数量 else (-1)*数量 end)from tab
group by 仓库,存货
popoisApig 2010-10-21
  • 打赏
  • 举报
回复

select cangku,cunhuo,shuliang=sum(case when biaoshi='收' then shuliang when biaoshi='发' then -shuliang end) from wcangku group by warehouse,cunhuo
popoisApig 2010-10-21
  • 打赏
  • 举报
回复
<code>
test code
</code>
popoisApig 2010-10-21
  • 打赏
  • 举报
回复
select cangku,cunhuo,shuliang=sum(case when biaoshi='收' then shuliang when biaoshi='发' then -shuliang end) from wcangku group by warehouse,cunhuo
幸运的意外 2010-10-21
  • 打赏
  • 举报
回复
没太有什么难点,只是用case when 判断一下收发就可以了。
select 仓库,存货,sum(case when 收发标志="收" then 数量 else (-1)*数量) as "现存数量"
from [table]
group by 仓库,存货
sql_cctv 2010-10-21
  • 打赏
  • 举报
回复
select 仓库,存货,数量=sum(case when 收发标志='收' then 数量 else (-1)*数量 end)from tab
group by 仓库,存货
claro 2010-10-21
  • 打赏
  • 举报
回复
补充:
with tbl as (
select 仓库,存货,(case when 收发标志='收' then 数量 else (-1)*数量) 数量 from tab
)
select 仓库,存货,sum(数量) from tbl group by 仓库,存货
bancxc 2010-10-21
  • 打赏
  • 举报
回复
 select 仓库,存货,数量=sum(case when 收发标志='收' then 数量 else (-1)*数量 end)from tab
group by 仓库,存货
fpzgm 2010-10-21
  • 打赏
  • 举报
回复

select 仓库,存货,sum(case when 收发标志='收' then 数量 else (-1)*数量) from a
group by 仓库,存货

加载更多回复(3)

34,838

社区成员

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

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