테이블에 필드가 1, 2, 3, 4 가 있다고 가정하고, 그 넷중에 하나라도 값이 있으면 보여주고 네개의 필드가 모두 null 이라면 안보여주게 하고 싶은데, 방법을 모르겠어요. 덧붙여, having 의 사용법도 좀 더 자세히 알고 싶습니다. 도와주세용!!
1> or 조건으로 묶어서 사용하면 됩니다.
select * from test
where
a is not null
or b is not null
or c is not null
or d is not null
2>having 은 group by한 결과를 다시 제한한때사용됩니다
create table #temp
(
c char(1),
d char(2)
)
insert #temp values ('1', 'a')
insert #temp values ('1', 'b')
insert #temp values ('2', 'c')
-- c 컬럼으로 묶었을때 건수가 1개인것을 찾기위해
select c, count(1) as cnt into #temp2 from #temp group by c
select * from #temp2
c cnt
- -----------
1 2
2 1
select c from #temp2 where cnt = 1
c
-
2
위 쿼리를
select c from #temp group by c having count(1) = 1
having 절을 사용하여 1개의 쿼리로 바꿀수 있습니다.
글쿤요... or로 수정 했습니다.
어떻게 된건지 and 로 해도 값이 똑같이 나옵니다. 신기하네요 ^^;
create table #temp ( pk char(1) not null, a char(1) null, b char(2) null, c char(2) null, d char(2) null )
insert #temp values ('1', null,null,null,null) insert #temp values ('2', '1',null,null,null) insert #temp values ('3', '1','2','3','4') insert #temp values ('4', '',null,null,null) 1> select * from #temp where a is not null or b is not null or c is not null or d is not null 결과 pk a b c d -- - - - - 2 1 NULL NULL NULL 3 1 2 3 4 4 NULL NULL NULL 2> select * from #temp where a is not null and b is not null and c is not null and d is not null 결과 pk a b c d -- - - - - 3 1 2 3 4 3> select * from #temp where rtrim(a) is not null or rtrim(b) is not null or rtrim(c) is not null or rtrim(d) is not null 결과 pk a b c d -- - - - - 2 1 NULL NULL NULL 3 1 2 3 4 각 컬럼에 null이 아니라 space가 들어가 있는 듯 합니다. 3번처럼 rtrim해서 사용하면 될 듯..