2013-11-22 2 views
-1

SQL 쿼리에 대한 도움이 필요합니다.일부 행만 반환하는 SQL 쿼리

테이블 :

사용자    일               형   값
존   월요일 청소기 0
존   월요일 청소기 1
베드로   월요일 청소기 1
마크,210 월요일 청소기 1
마크   월요일 청소기 0

내가 값 필드> 0이있는 행을 반환하는 쿼리를 원하지만 행이있는 경우 0 값을 가진 사용자 낮과 유형으로 어떤 행이 반환된다 . 값 0과 1 및 마크가있는 "존 월요일 청소기"가 있기 때문에이 경우에만 "peter monday cleaner 1"행만 반환됩니다.

감사

+4

사용 된 SQL 쿼리를 보자. – Andy

답변

0

를 I 귀하의 요구 사항 설명에서 이것이 귀하가 필요로하는 질문임을 이해했습니다.

select * from table a where a.value>0 
// This returns all rows that have values equal to 1. In your case 
     john monday cleaner 1 
     peter monday cleaner 1 
     mark monday cleaner 1 


    and a.user not in 
    (select b.user from table b where b.value=0) 

// This would check that the users in the above 3 rows do not have any rows in the 
    result of the select with value =0 

     john monday cleaner 0 
     mark monday cleaner 0 

//Here it has 2 rows with value = 0 and "and a.user not in" would esnure that from 
//the first 3 rows, only those users are selected who are not fetched in the 2 rows 
//given above. 

그래서 당신은 U이 필요로 정확히 이잖아, 쿼리

select * from table a where a.value>0 
and a.user not in 
(select b.user from table b where b.value=0) 

peter monday cleaner 1 // Result. 

내 생각을 실행할 때.

+0

@ user3022110 내 쿼리가 편집되었습니다. 지금 해보십시오. – Adarsh

+0

너무 빨리 말하면 첫 번째 경기에서만 작동합니다. 동일한 조건의 행이 여러 개 있고 사용자 요일과 유형 필드가 0과 1이 같은 값일 경우 값이 1 인 행을 표시합니다. – user3022110

+0

해당 행을 사용하여 질문을 업데이트 할 수 있습니까? – Adarsh

1

보십시오이 :

SELECT * FROM Table as a WHERE a.value > 0 AND EXISTS(SELECT 1 FROM Table as b WHERE b.user=a.user AND b.value=0) 

당신이 다른 번호와 같은 행을 원하는 경우에만 사용할 수 있습니다 일단 표시 : 어떤에서

SELECT DISTINCT a.user, a.day, a.type FROM Table as a WHERE a.value > 0 AND EXISTS(SELECT 1 FROM Table as b WHERE b.user=a.user AND b.value=0) 
+0

존재하지 않아야합니까? 그는 값이 0 인 행이 없으면 값이 0보다 큰 행만 표시하려고합니다. – Adarsh

+0

값이 0 인 행을 원한다는 것을 이해했습니다. – Perry

+0

"SELECT * FROM Table as a a a.value > 0 AND EXISTS (테이블에서 b를 선택하면 b.user = a.user 및 b.value = 0) "는"john monday cleaner 1 "과"peter monday cleaner 1 "을 나타내며"peter monday 클리너 1 " – user3022110

0
select _user,_day,_type,MAX(_Value) 
    from #tmp 
    GROUP BY _user,_day,_type 
    having COUNT(*) = 1 
관련 문제