表名 Table字段 库存id
客户名称
数量
入厂
出厂
数据如下:库存id 客户名称 数量 入厂 出厂
988 北京现代 8 1 0 ‘这条记录代表入厂8个
---------------------------------
988 北京现代 3 0 1 ‘这条记录代表出厂3个
988 北京现代 5 0 1 ‘这条记录代表出厂5个
-----------------------------------
988 北京现代 2 1 0 ‘这条记录代表入厂2个986 北京奔驰 2 0 1 ‘这条记录代表出厂2个我的要求是 如果同样一个库存id 入厂和出厂数量一致 查询就不显示记录.上面的查询结果应该是
库存id 客户名称 数量 入厂 出厂
986 北京奔驰 2 0 1
988 北京现代 2 1 0
客户名称
数量
入厂
出厂
数据如下:库存id 客户名称 数量 入厂 出厂
988 北京现代 8 1 0 ‘这条记录代表入厂8个
---------------------------------
988 北京现代 3 0 1 ‘这条记录代表出厂3个
988 北京现代 5 0 1 ‘这条记录代表出厂5个
-----------------------------------
988 北京现代 2 1 0 ‘这条记录代表入厂2个986 北京奔驰 2 0 1 ‘这条记录代表出厂2个我的要求是 如果同样一个库存id 入厂和出厂数量一致 查询就不显示记录.上面的查询结果应该是
库存id 客户名称 数量 入厂 出厂
986 北京奔驰 2 0 1
988 北京现代 2 1 0
986 北京奔驰 2 0 1
988 北京现代 2 1 0
select * from #tb a
where not exists
(select 1 from #tb b where a.库存id=b.库存id
and a.数量=b.数量 and (a.入厂=b.出厂 and b.入厂=a.出厂))
USE SSISTest;
GO
SET NOCOUNT ON
IF OBJECT_ID(N'A',N'U')IS NOT NULL DROP TABLE A
GOCREATE TABLE A--创建测试数据表
(
Storeid int not null,
Name nvarchar(100),
Number int ,
InF int,
OutF int
)
INSERT INTO A--插入测试数据
select 988 ,'北京现代', 8, 1, 0 union all-- ‘这条记录代表入厂8个
---------------------------------
select 988, '北京现代', 3, 0, 1 union all --‘这条记录代表出厂3个
select 988, '北京现代', 5, 0, 1 union all-- ‘这条记录代表出厂5个
-----------------------------------
select 988, '北京现代', 2, 1, 0 union all-- ‘这条记录代表入厂2个select 986, '北京奔驰', 2, 0, 1 -- ‘这条记录代表出厂2个go;with cte1 as
(
select
Storeid,
case when OutF=1 then (0-Number)
else Number end as Number,
Inf,
OutF,
row_number()over(partition by Storeid order by getdate()) as RN
from A
)
,cte2 as
(
select sum(Number) as TheSum,Storeid from cte1
group by Storeid
)
select
cte1.Storeid,
case when cte1.Number<0 then (0-cte1.Number)
else cte1.Number end as Number,
cte1.InF,
cte1.OutF
from cte2
inner join cte1 on cte2.TheSum=cte1.Number and cte1.Storeid=cte2.Storeid
/*
Storeid Number InF OutF
----------- ----------- ----------- -----------
986 2 0 1
988 2 1 0
*/
create table t1
(
id int,
name varchar(20),
amount int,
ru int,
chu int
)
insert into t1
select 988, '北京现代', 8, 1, 0 union all
select 988, '北京现代', 3, 0, 1 union all
select 988, '北京现代', 5, 0, 1 union all
select 988, '北京现代', 2, 1, 0 union all
select 986, '北京奔驰', 2, 0, 1
select * from t1;with aaa as
(select id,name,SUM(case when ru=1 then amount else 0 end) as ruamount,SUM(case when chu=1 then amount else 0 end) as chuamount
from t1 group by id,name)
,bbb as
(select id,name,abs(ruamount-chuamount) as amount,case when ruamount-chuamount>0 then 1 else 0 end as ru,
case when ruamount-chuamount<0 then 1 else 0 end as chu from aaa)
select * from bbb
go
create table #tt([KCID] int,[KHMC] varchar(8),[FQty] int,[inPut] int,[OutPut] int)
insert #tt
select 988,'北京现代',8,1,0 union all
select 988,'北京现代',3,0,1 union all
select 988,'北京现代',5,0,1 union all
select 988,'北京现代',2,1,0 union all
select 986,'北京奔驰',2,0,1select * from #ttSELECT kcid,KHMC,SUM(CASE WHEN INPUT=1 THEN 1 ELSE 0 end) AS pin,SUM(CASE WHEN OutPut=1 THEN 1 ELSE 0 end) AS Pout FROM #tt
GROUP BY KCID,KHMCDROP TABLE #tt