2013-06-20 43 views
0

我的查询,这样的作品是如下条件:访问SQL - 多或使用复选框

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子句试图确定上述三个复选框(扫盲,算术或贫穷)。三者的任何组合都可以检查我想要的结果。

原则上,查询起作用。但是,输出结果返回了第三部分的两个结果和第四部分的两个结果。

如果我运行定义只是一个复选框的查询,以便:

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

查询工作正常,它只是在添加一个或多个条件似乎会导致问题。

我也尝试通过使用= -1定义真值,它返回相同的问题。

非常感谢。

回答

2

看看这是否更清楚。您不需要IIF函数来检查WHERE子句中的是/否值。的方法,另外,括号中需要表达的逻辑:(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