请教:返回要求数据列表~
CREATE TABLE [ww] (
[a] [varchar] (4) COLLATE Chinese_PRC_CI_AS NULL ,
[b] [char] (2) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
insert into ww
select '001 ', '0 '
union all
select '001 ', '1 '
union all
select '001 ', '2 '
union all
select '002 ', '0 '
union all
select '003 ', '0 '
union all
select '003 ', '1 '
union all
select '003 ', '2 '
要求 只要b列大于0的话 就不要列出b=0的那行
要得到的结果是 麻烦大家了~~
0011
0012
0020
0031
0032
[解决办法]
select * from ww a
where not exists (select 1 from ww where a.a=a group by a having max(b)> 0)
or b <> 0
[解决办法]
try:
CREATE TABLE [ww] (
[a] [varchar] (4) COLLATE Chinese_PRC_CI_AS NULL ,
[b] [char] (2) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
insert into ww
select '001 ', '0 '
union all
select '001 ', '1 '
union all
select '001 ', '2 '
union all
select '002 ', '0 '
union all
select '003 ', '0 '
union all
select '003 ', '1 '
union all
select '003 ', '2 '
SELECT * FROM ww AS A WHERE b> 0
OR (b=0 AND NOT EXISTS(SELECT 1 FROM ww AS B WHERE B.a=A.a GROUP BY B.a HAVING COUNT(1)> 1))
/*
0011
0012
0020
0031
0032
*/
DROP TABLE ww