27,579
社区成员
发帖
与我相关
我的任务
分享
LZ说如果是M的时候,只要sum(qty)〉0,就取出所有的
if object_id('tb') is not null
drop table tb
go
create table tb(id int,set_id varchar(20),bind_type varchar(20),qty int)
insert into tb select 1,'s1','N',12
insert into tb select 2,'s2','N',10
insert into tb select 3,'s3','N',0
insert into tb select 4,'S4','M',34
insert into tb select 5,'S4','M',0
insert into tb select 6,'S4','M',0
select * from tb
where (bind_type='N' and qty>0) or
(exists (select 1 from tb a where a.bind_type='M' and a.set_id=tb.set_id
group by set_id having sum(qty)>0 ))
/*
1 s1 N 12
2 s2 N 10
4 S4 M 34
5 S4 M 0
6 S4 M 0
*/
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (id int,set_id varchar(2),bind_type varchar(1),qty int)
insert into #T
select 1,'s1','N',12 union all
select 2,'s2','N',10 union all
select 3,'s3','N',0 union all
select 4,'s4','M',34 union all
select 5,'S4','M',10 union all
select 6,'S4','M',9 union all
select 6,'S5','M',0 union all
select 6,'S5','M',0 union all
select 6,'S5','M',0
select * from (
select id,set_id,bind_type,
case when bind_type ='N' and qty <> '0' then qty
when bind_type ='N' and qty = '0' then null
when bind_type='M' then
case when (select isnull(sum(qty),0) from #t where set_id=a.set_id) =0 then null
else
case when id=(select min(id) from #t where set_id=a.set_id) then (select sum(qty) from #t where set_id=a.set_id)
else 0
end
end
end as qty
from #T a
) a where qty is not null
/*
------------------
1 s1 N 12
2 s2 N 10
4 s4 M 53
5 S4 M 0
6 S4 M 0
*/
if object_id('tb') is not null
drop table tb
go
create table tb(id int,set_id varchar(20),bind_type varchar(20),qty int)
insert into tb select 1,'s1','N',12
insert into tb select 2,'s2','N',10
insert into tb select 3,'s3','N',0
insert into tb select 7,'s3','N',1
insert into tb select 4,'S4','M',34
insert into tb select 5,'S4','M',0
insert into tb select 6,'S4','M',0
select * from tb t
where not exists(
select 1 from tb where set_id=t.set_id and t.bind_type='m' group by set_id having sum(qty)=0
) and not (bind_type='n' and qty=0)
-->生成测试数据
DECLARE @TB TABLE([ID] INT,[SET_ID] NVARCHAR(2),[BIND_TYPE] NVARCHAR(1),[QTY] INT)
INSERT @TB
SELECT 1,N'S1',N'N',N'12' UNION ALL
SELECT 2,N'S2',N'N',N'10' UNION ALL
SELECT 3,N'S3',N'N',N'0' UNION ALL
SELECT 4,N'S4',N'M',N'34' UNION ALL
SELECT 5,N'S4',N'M',N'0' UNION ALL
SELECT 6,N'S4',N'M',N'0'
SELECT * FROM @TB T
WHERE NOT EXISTS(
SELECT 1 FROM @TB WHERE [SET_ID]=T.[SET_ID] GROUP BY SET_ID HAVING SUM([QTY])=0
)
create table tb (id int,set_id varchar(10),bind_type varchar(10) ,qty int)
insert into tb select 1 , 's1' , 'N' , 12
insert into tb select 2 , 's2' , 'N' , 10
insert into tb select 3 , 's3' , 'N' , 0
insert into tb select 4 , 's4' , 'M' , 34
insert into tb select 5 , 'S4' , 'M' , 0
insert into tb select 6 , 'S4' , 'M' , 0
select id,set_id,bind_type,qty from tb where bind_type = 'N'and qty<>0
union all
select id,set_id,bind_type,sum(qty) qty from tb where bind_type = 'M'
group by id,set_id,bind_type
/*
id set_id bind_type qty
1 s1 N 12
2 s2 N 10
4 s4 M 34
5 S4 M 0
6 S4 M 0
*/
drop table tb
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (id int,set_id varchar(2),bind_type varchar(1),qty int)
insert into #T
select 1,'s1','N',12 union all
select 2,'s2','N',10 union all
select 3,'s3','N',0 union all
select 4,'s4','M',34 union all
select 5,'S4','M',10 union all
select 6,'S4','M',9
select * from (
select id,set_id,bind_type,
case when bind_type ='N' and qty <> '0' then qty
when bind_type ='N' and qty = '0' then null
when bind_type='M' then
case when id=(select min(id) from #t where set_id=a.set_id) then (select sum(qty) from #t where set_id=a.set_id)
else 0
end
end as qty
from #T a
) a where qty is not null
/*
------------------
1 s1 N 12
2 s2 N 10
4 s4 M 53
5 S4 M 0
6 S4 M 0
*/
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (id int,set_id varchar(2),bind_type varchar(1),qty int)
insert into #T
select 1,'s1','N',12 union all
select 2,'s2','N',10 union all
select 3,'s3','N',0 union all
select 4,'s4','M',34 union all
select 5,'S4','M',0 union all
select 6,'S4','M',0
select * from (
select id,set_id,bind_type,
case when bind_type ='N' and qty <> '0' then qty
when bind_type ='N' and qty = '0' then null
when bind_type='M' then
case when id=(select min(id) from #t where set_id=a.set_id) then (select sum(qty) from #t where set_id=a.set_id)
else 0
end
end as qty
from #T a
) a where qty is not null
/*
------------------
1 s1 N 12
2 s2 N 10
4 s4 M 34
5 S4 M 0
6 S4 M 0
*/
if object_id('tb') is not null
drop table tb
go
create table tb(id int,set_id varchar(20),bind_type varchar(20),qty int)
insert into tb select 1,'s1','N',12
insert into tb select 2,'s2','N',10
insert into tb select 3,'s3','N',0
insert into tb select 4,'S4','M',34
insert into tb select 5,'S4','M',0
insert into tb select 6,'S4','M',0
select * from tb t
where not exists(
select 1 from tb where set_id=t.set_id group by set_id having sum(qty)=0
)