日期:2014-05-18  浏览次数:20478 次

请教:返回要求数据列表~
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的那行  
要得到的结果是   麻烦大家了~~
001 1  
001 2  
002 0  
003 1  
003 2  




------解决方案--------------------

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))
/*

001 1
001 2
002 0
003 1
003 2
*/
DROP TABLE ww