访问2007 SQL查询以忽略0值

时间:2013-03-06 12:57:53

标签: sql-server ms-access-2007

我试图通过日期报告来构建我正在使用的代码,但不是我需要它做的事情。

SELECT Notices.Promoter, 
Sum(IIf([Notices].[Type]='GRANT PERMIT' Or [Notices].[Type]='GRANT VARIATION' Or [Notices].[Notice Type]='GRANT PAA',1,0)) AS Granted, 
Sum(IIf([Notices].[Type]='REFUSE    APPLICATION',1,0)) AS Refused, 
Sum(IIf([Notices].[Status]='Deemed',1,0)) AS Deemed, 
Sum(IIf([Notices].[Error]<>"" And Notices.[Category]<>"Observation" And Notices.[Category]<>"S.74 Overrun",1,0)) AS [Potential Penalty]
FROM Notices
WHERE (((Notices.[Day Of Week])=[TempVars]![DayReport]))
GROUP BY Notices.Promoter;

tempVar [DayReport]是星期一,星期二,星期三......太阳 并且代码输出此

Promoter | Granted | Refused | Deemed | Potential Penalty
Name     |   0     |    0    |    0   |   0
Name2    |   3     |    0    |    0   |   0
Name3    |   4     |    2    |    1   |   0
Name4    |   0     |    1    |    1   |   0
Name5    |   1     |    0    |    0   |   0

我想要的是不要在这样的字段中显示具有全0的启动器

Promoter | Granted | Refused | Deemed | Potential Penalty
Name2    |   3     |    0    |    0   |   0
Name3    |   4     |    2    |    1   |   0
Name4    |   0     |    1    |    1   |   0
Name5    |   1     |    0    |    0   |   0

由于我不确定如何解决这个问题,我想我会把它交给Stackoverflow的优秀人才

1 个答案:

答案 0 :(得分:0)

<击>

<击>
SELECT  *
FROM
(
    SELECT  Notices.Promoter, 
            Sum(IIf([Notices].[Type]='GRANT PERMIT' Or [Notices].[Type]='GRANT VARIATION' Or [Notices].[Notice Type]='GRANT PAA',1,0)) AS Granted, 
            Sum(IIf([Notices].[Type]='REFUSE    APPLICATION',1,0)) AS Refused, 
            Sum(IIf([Notices].[Status]='Deemed',1,0)) AS Deemed, 
            Sum(IIf([Notices].[Error]<>"" And Notices.[Category]<>"Observation" And Notices.[Category]<>"S.74 Overrun",1,0)) AS [Potential Penalty]
    FROM    Notices
    WHERE   (((Notices.[Day Of Week])=[TempVars]![DayReport]))
    GROUP   BY Notices.Promoter
) s
WHERE   Promoter <> 0
        Refused <> 0 AND 
        Deemed <> 0 AND 
        [Potential Penalty] <> 0

<击>

更新1

SELECT  Notices.Promoter, 
        Sum(IIf([Notices].[Type]='GRANT PERMIT' Or [Notices].[Type]='GRANT VARIATION' Or [Notices].[Notice Type]='GRANT PAA',1,0)) AS Granted, 
        Sum(IIf([Notices].[Type]='REFUSE    APPLICATION',1,0)) AS Refused, 
        Sum(IIf([Notices].[Status]='Deemed',1,0)) AS Deemed, 
        Sum(IIf([Notices].[Error]<>"" And Notices.[Category]<>"Observation" And Notices.[Category]<>"S.74 Overrun",1,0)) AS [Potential Penalty]
FROM    Notices
WHERE   (((Notices.[Day Of Week])=[TempVars]![DayReport]))
GROUP   BY Notices.Promoter
HAVING  Sum(IIf([Notices].[Type]='GRANT PERMIT' Or [Notices].[Type]='GRANT VARIATION' Or [Notices].[Notice Type]='GRANT PAA',1,0)) <> 0 AND
        Sum(IIf([Notices].[Type]='REFUSE    APPLICATION',1,0)) <> 0 AND
        Sum(IIf([Notices].[Status]='Deemed',1,0)) <> 0 AND
        Sum(IIf([Notices].[Error]<>"" And Notices.[Category]<>"Observation" And Notices.[Category]<>"S.74 Overrun",1,0)) <> 0