34,838
社区成员




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 行受影响)
*/
select 仓库,存货,sum(case when 收发标志='收' then 数量 else (-1)*数量 end)as 数量 from tab
group by 仓库,存货
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
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
-----------------------------*/
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
select 仓库,存货,数量=sum(case when 收发标志='收' then 数量 else (-1)*数量 end)from tab
group by 仓库,存货
select cangku,cunhuo,shuliang=sum(case when biaoshi='收' then shuliang when biaoshi='发' then -shuliang end) from wcangku group by warehouse,cunhuo
select 仓库,存货,数量=sum(case when 收发标志='收' then 数量 else (-1)*数量 end)from tab
group by 仓库,存货
with tbl as (
select 仓库,存货,(case when 收发标志='收' then 数量 else (-1)*数量) 数量 from tab
)
select 仓库,存货,sum(数量) from tbl group by 仓库,存货
select 仓库,存货,数量=sum(case when 收发标志='收' then 数量 else (-1)*数量 end)from tab
group by 仓库,存货
select 仓库,存货,sum(case when 收发标志='收' then 数量 else (-1)*数量) from a
group by 仓库,存货