现有库存表 a 编号 数量 有500条记录
盘存表b 编号 数量 有480 条数据(也有可能盘点数量超过库存表的数量,比如说盘点有501个记录)
如何通过表a 和表b 查询出这次盘点的货多货少?
盘存表b 编号 数量 有480 条数据(也有可能盘点数量超过库存表的数量,比如说盘点有501个记录)
如何通过表a 和表b 查询出这次盘点的货多货少?
解决方案 »
- 为什么这两条SELECT语句可以通过,但是换成DELETE之后就出错了呢?
- 问一下,我现在要查看一个数据库的日志文件,我要用什么软件才可以看到啊
- 写条update语句
- 用alter table 添加字段的时候,字段名是否能使用变量呢?
- 链接服务器 "ITSV" 的 OLE DB 访问接口 "SQLNCLI10"
- LogExplore 怎么恢复ntext 字段的数据,我恢复后发现数据类型是varchar(3000),并且数据被截断了。
- Incorrect syntax near the keyword 'SELECT'.
- 假如我现在插入了N条记录!怎样获得刚插入N条记录的自动编号ID的值
- 很简单的问题,进来就有分了.
- 在sql server 中如何插入图片?
- 求教简洁SQL语句写法, 高手入..
- 一道SQL题
when a.库存量=b.盘存量 then '无差错'
when a.库存量>b.盘存量 then '货少' end
from (select id ,库存量=count(*) from a)s join
(select id ,盘存量=count(*) from b)t on s.id=t.id
case when isnull(m.数量,0) - isnull(n.数量,0) = 0 then '无差错'
when isnull(m.数量,0) - isnull(n.数量,0) > 0 then '货少'
when isnull(m.数量,0) - isnull(n.数量,0) < 0 then '货多'
end '盘点情况'
from
(select 编号 , sum(数量) 数量 from a group by 编号) m
full join
(select 编号 , sum(数量) 数量 from b group by 编号) n
on m.编号 = n.编号
order by 编号
select isnull(m.编号 , n.编号) 编号 ,
case when isnull(m.数量,0) - isnull(n.数量,0) = 0 then '无差错'
when isnull(m.数量,0) - isnull(n.数量,0) > 0 then '货少'
when isnull(m.数量,0) - isnull(n.数量,0) < 0 then '货多'
end '盘点情况',
isnull(m.数量,0) - isnull(n.数量,0) 数量
from
(select 编号 , sum(数量) 数量 from a group by 编号) m
full join
(select 编号 , sum(数量) 数量 from b group by 编号) n
on m.编号 = n.编号
order by 编号
create table a(bh varchar(10), total int)
insert into a
select
'001',1001 union all select
'001',801 union all select
'002', 879 union all select
'003', 30;create table b(bh varchar(10), total int);
insert into b
select
'001',991 union all select
'001',991 union all select
'002', 881 union all select
'003', 26;select bh '编号', (case when sum(total)>0 then '盘赢 '+convert(varchar(10),sum(total))+' 件'
when sum(total)=0 then '准确'
else '盘亏 '+convert(varchar(10),-sum(total))+' 件' end)
as '盘点结果'
from (
select a.bh, a.total from a
union all
select b.bh, -b.total from b) t
group by t.bh
-----------------------------------
编号 盘点结果
001 盘亏 180 件
002 盘亏 2 件
003 盘赢 4 件
------------如果两表中的编号(bh)字段记录不唯一,则先汇总-------------
create table a(bh varchar(10), total int)
insert into a
select
'001',1001 union all select
'001',801 union all select
'002', 879 union all select
'003', 30;create table b(bh varchar(10), total int);
insert into b
select
'001',991 union all select
'001',991 union all select
'002', 881 union all select
'003', 26;select bh '编号', (case when sum(total)>0 then '盘亏 '+convert(varchar(10),sum(total))+' 件'
when sum(total)=0 then '准确'
else '盘赢 '+convert(varchar(10),-sum(total))+' 件' end)
as '盘点结果'
from (
select a.bh, a.total from a
union all
select b.bh, -b.total from b) t
group by t.bh
-----------------------------------
编号 盘点结果
001 盘赢 180 件
002 盘赢 2 件
003 盘亏 4 件
create table a(bh varchar(10), total int)
insert into a
select
'001',1001 union all select
'001',801 union all select
'002', 879 union all select
'003', 30 union all select
'004', 27;create table b(bh varchar(10), total int);
insert into b
select
'001',991 union all select
'001',991 union all select
'002', 881 union all select
'003', 26 union all select
'004', 27;select bh '编号', (case when sum(total)>0 then '盘亏 '+convert(varchar(10),sum(total))+' 件'
when sum(total)=0 then '准确'
else '盘赢 '+convert(varchar(10),-sum(total))+' 件' end)
as '盘点结果'
from (
select a.bh, a.total from a
union all
select b.bh, -b.total from b) t
group by t.bh
-----------------------------------
编号 盘点结果
001 盘赢 180 件
002 盘赢 2 件
003 盘亏 4 件
004 准确