table A
名称 数量
a 3
a 4
b 9
b 4
c 5table B
名称 数量
a 7
b 6
b 9
d 4
e 2如何计算用table A数量减去table B并去掉0的结果,如下显示
名称 A数量 B数量 A-B
b 13 15 -2
c 5 0 5
d 0 4 -4
e 0 2 -2
名称 数量
a 3
a 4
b 9
b 4
c 5table B
名称 数量
a 7
b 6
b 9
d 4
e 2如何计算用table A数量减去table B并去掉0的结果,如下显示
名称 A数量 B数量 A-B
b 13 15 -2
c 5 0 5
d 0 4 -4
e 0 2 -2
解决方案 »
- 关于SQL 2005的安装问题
- 请教安装的问题
- 重复记录 ; distinct ;Text类型 的问题
- 程序越编越大,修改时只是面对一大堆vb代码翻来翻去很不方便,到底写软件走怎样的路子才对,修改才容易?是从需求分析-概要设计-详细设计-...这样走下去呢,还是随便一点好呢。哪有一个开发模式可供参考参考,从分析到设计再到实现、测试等。怎样才能理清开发思路,修
- 很急的问题,请你帮帮忙,送分
- 大家来玩SQL语句
- MSSQL 触发器 怎么修改记录的值?
- 这个SQL应该怎样写?
- 在进行数据查询时,输入查询条件时如何象在IE浏览器输入网址那样有相同的字符则会自动出现由你选择!
- sql保存子id
- sql语句如何去重保留一条记录啊
- SQL Server2008问题多多!!求一个好用SQL Server数据库软件
with tb1(名称,数量)
as(select 'a',3 union all
select 'a',4 union all
select 'b',9 union all
select 'b',4 union all
select 'c',5),
tb2(名称,数量)
as(
select 'a',7 union all
select 'b',6 union all
select 'b',9 union all
select 'd',4 union all
select 'e',2),
source1 as(
select 名称 name,sum(数量) [count] from tb1 group by 名称
),
source2 as(
select 名称 name,sum(数量) [count] from tb2 group by 名称
),
source as(
select a.name 名称,a.count A数量,b.count B数量,a.count-b.count AB from source1 a,source2 b where a.name=b.name
union all
select b.name 名称,0 A数量,b.count B数量,0-b.count AB from source2 b where (select count(1) from source1 a where a.name=b.name)<=0
union all
select a.name 名称,a.count A数量,0 B数量,a.count AB from source1 a where (select count(1) from source2 b where a.name=b.name)<=0
)
select * from source where [AB]!=0 order by 名称
--创建测试数据
if OBJECT_ID('tb1_A') is not null drop table tb1_A
create table tb1_A(名称 nvarchar(10),数量 int)
go
insert into tb1_A
select 'a',3 union all
select 'a',4 union all
select 'b',9 union all
select 'b',4 union all
select 'c',5if OBJECT_ID('tb1_B') is not null drop table tb1_B
create table tb1_B(名称 nvarchar(10),数量 int)
go
insert into tb1_B
select 'a',7 union all
select 'b',6 union all
select 'b',9 union all
select 'd',4 union all
select 'e',2
go--查询
with T as(
select 名称,sum(数量) 数量,'A' [Type] from tb1_A group by 名称
union all
select 名称,sum(数量) 数量,'B' [Type] from tb1_B group by 名称
)
select 名称,isnull(A,0) A数量,isnull(B,0) B数量,isnull(A,0)-isnull(B,0) [A-B]
from T pivot(sum(数量) for [Type] in(A,B))p
where isnull(A,0)<>isnull(B,0)/**结果
名称 A数量 B数量 A-B
---------- ----------- ----------- -----------
b 13 15 -2
c 5 0 5
d 0 4 -4
e 0 2 -2(4 行受影响)
**/
go
-->生成表tableA
--if object_id('tableA') is not null
-- drop table tableA
--Go
Create table tableA([名称] nvarchar(1),[数量] smallint)
Insert into tableA
Select N'a',3
Union all Select N'a',4
Union all Select N'b',9
Union all Select N'b',4
Union all Select N'c',5
-->生成表tableB
--if object_id('tableB') is not null
-- drop table tableB
--Go
Create table tableB([名称] nvarchar(1),[数量] smallint)
Insert into tableB
Select N'a',7
Union all Select N'b',6
Union all Select N'b',9
Union all Select N'd',4
Union all Select N'e',2SELECT
COALESCE(a.名称,b.名称) AS 名称
,ISNULL(a.数量,0) AS A数量
,ISNULL(b.数量,0) AS B数量
,ISNULL(a.数量,0)-ISNULL(b.数量,0) AS [A-B]
FROM (
SELECT
名称,SUM(数量) AS 数量
FROM tableA
GROUP BY 名称
) AS a
FULL JOIN (
SELECT
名称,SUM(数量) AS 数量
FROM tableB
GROUP BY 名称
) AS b ON a.名称=b.名称
WHERE ISNULL(a.数量,0)-ISNULL(b.数量,0)<>0
Go
create table testb(mc nvarchar(10),qty int)
insert testa select 'a',3
insert testa select 'a',4
insert testa select 'b',9
insert testa select 'b',4
insert testa select 'c',5insert testb select 'a',7
insert testb select 'b',6
insert testb select 'b',9
insert testb select 'd',4
insert testb select 'e',2select isnull(b.mc,a.mc) 名称,isnull(a.qty,0) A数量,isnull(b.qty,0) B数量,isnull(a.qty,0)-isnull(b.qty,0) 'A-B' from (select mc,sum(qty) qty from testb
group by mc) b full join (select mc,sum(qty) qty from testa
group by mc) a on a.mc=b.mc where isnull(a.qty,0)-isnull(b.qty,0)<>0
二、union之后去掉数量=0的记录
select 名称,sum(数量)
from
(
select 名称,sum(数量)from a group by 名称
union all
select 名称,-sum(数量)from b group by 名称
) t
where 数量>0
group by 名称