2013-06-20 3 views
0

다음과 같다 작품의 종류 내 쿼리, 사용 조건 :액세스 SQL - 여러 OR 체크 박스

이 쿼리의 모든 네 부분의 WHERE 절은 세 가지 언급 체크 박스를 결정하기 위해 노력하고있다
SELECT [copyright status], 
sum(IIF(layer='key info',1,0)) AS [Key Info], 
sum(IIF(layer='approaches',1,0)) AS [Approaches], 
sum(IIF(layer='research',1,0)) AS [Research] 
FROM resources 
WHERE (IIF(literacy,1,0)) OR (IIF(numeracy,1,0)) OR (IIF(poverty,1,0)) 
GROUP BY [copyright status] 

UNION 

SELECT null, 
sum(IIF(layer='key info',1,0)) AS [Key Info], 
sum(IIF(layer='approaches',1,0)) AS [Approaches], 
sum(IIF(layer='research',1,0)) AS [Research] 
FROM resources 
WHERE (IIF(literacy,1,0)) OR (IIF(numeracy,1,0)) OR (IIF(poverty,1,0)) 

UNION 

SELECT [lw status], 
sum(IIF(layer='key info',1,0)) AS [Key Info], 
sum(IIF(layer='approaches',1,0)) AS [Approaches], 
sum(IIF(layer='research',1,0)) AS [Research] 
FROM resources 
WHERE (IIF(literacy,1,0)) OR (IIF(numeracy,1,0)) OR (IIF(poverty,1,0)) AND [lw status] = 'In Reserve' 
GROUP BY [lw status] 

UNION 

SELECT [lw status], 
sum(IIF(layer='key info',1,0)) AS [Key Info], 
sum(IIF(layer='approaches',1,0)) AS [Approaches], 
sum(IIF(layer='research',1,0)) AS [Research] 
FROM resources 
WHERE (IIF(literacy,1,0)) OR (IIF(numeracy,1,0)) OR (IIF(poverty,1,0)) AND [lw status] = 'Published' 
GROUP BY [lw status]; 

(문맹 퇴치를 , 수리력 또는 빈곤)이 점검됩니다. 세 가지 조합을 통해 내가 원하는 결과를 확인할 수 있습니다.

기본적으로 쿼리가 작동합니다. 그러나 출력은 세 번째 부분에 대해 두 개의 결과를 반환하고 네 번째 부분에 대해 두 개의 결과를 반환합니다. 그래서

내가 정의 된 하나 개의 체크 박스에 쿼리를 실행하면

는 : 쿼리가 잘 작동

WHERE (IIF(literacy,1,0)) [lw status] = 'In Reserve' 

그 단지에 추가 이러한 조건 중 하나 이상이 문제가 발생할 것으로 보인다.

같은 문제를 반환하는 = -1을 사용하여 참값을 정의 해 보았습니다.

감사합니다.

답변

2

이것이 더 명확한 지 확인하십시오. WHERE 절에서 Yes/No 값을 확인하기 위해 IIF 함수가 필요하지 않습니다. Aditionally, 괄호는 논리를 표현하기 위해 필요합니다 (x OR y OR z) AND w

SELECT null as Status, 
sum(IIF(layer='key info',1,0)) AS [Key Info], 
sum(IIF(layer='approaches',1,0)) AS [Approaches], 
sum(IIF(layer='research',1,0)) AS [Research] 
FROM resources 
WHERE (literacy OR numeracy OR poverty) 

UNION 

SELECT [copyright status], 
sum(IIF(layer='key info',1,0)) AS [Key Info], 
sum(IIF(layer='approaches',1,0)) AS [Approaches], 
sum(IIF(layer='research',1,0)) AS [Research] 
FROM resources 
WHERE (literacy OR numeracy OR poverty) 
GROUP BY [copyright status] 

UNION 

SELECT [lw status], 
sum(IIF(layer='key info',1,0)) AS [Key Info], 
sum(IIF(layer='approaches',1,0)) AS [Approaches], 
sum(IIF(layer='research',1,0)) AS [Research] 
FROM resources 
WHERE (literacy OR numeracy OR poverty) AND [lw status] = 'In Reserve' 
GROUP BY [lw status] 

UNION 

SELECT [lw status], 
sum(IIF(layer='key info',1,0)) AS [Key Info], 
sum(IIF(layer='approaches',1,0)) AS [Approaches], 
sum(IIF(layer='research',1,0)) AS [Research] 
FROM resources 
WHERE (literacy OR numeracy OR poverty) AND [lw status] = 'Published' 
GROUP BY [lw status]; 

감사

+0

놀라운, 감사하고에 설명 주셔서 감사합니다. SQL 학습에 대한 길은 계속됩니다. – Squadinho