我有一个报告跟踪某些项目在数据库中的持续时间,并通过一系列年龄范围(20-44,45-60,61-90,91-180,180 +)跟踪它)。我有以下查询作为报告的数据源:
SELECT DISTINCT Source.ItemName,
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) > 20) AS Total,
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) BETWEEN 20 AND 44) AS BTWN_20_44,
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) BETWEEN 45 AND 60) AS BTWN_45_60,
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) BETWEEN 61 AND 90) AS BTWN_61_90,
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) BETWEEN 91 AND 180) AS BTWN_91_180,
Count(SELECT Source.DateAdded FROM Source WHERE Int(Date()-Source.DateAdded) > 180) AS GT_180
FROM Source
GROUP BY Source.ItemName;
此查询效果很好,除非列中没有任何条目。而不是返回0的计数,返回一个空值。
如何让Count()返回0而不是空?
答案 0 :(得分:9)
您可以返回
ISNULL(Count(......), 0)
一切都应该没问题 - 将在MS SQL Server中 - 但我刚看到你正在使用Access。由于我不太了解Access,我不确定这会起作用 - 你可以试试吗?
好的 - 很高兴看到Access中有类似的内容(如果与SQL Server中的内容完全不同)。
马克
答案 1 :(得分:1)
更好的是,使用Nz()
例如
Nz(Count(SELECT Source.DateAdded
FROM Source
WHERE Int(Date()-Source.DateAdded), 0)
当结果为null时返回0,否则返回。
请注意Nz()
函数是Access对象模型的一部分,因此仅在Access用户界面中使用时才可用。如果您使用没有Access UI的Access数据库引擎(通过OLE DB,ODBC等从另一个应用程序),那么您将收到错误,“表达式中未定义的函数'Nz'。”
答案 2 :(得分:1)
第二个虽然(没有更多信息)我认为你这个查询非常错误....
正如我所说,当我尝试原始查询时,我收到错误“此子查询最多可以返回一条记录”。
这可能更接近你想要的东西
SELECT DISTINCT Source.ItemName,
(SELECT count(Source.DateAdded ) FROM Source
WHERE Int(Date()-Source.DateAdded)> 20) AS Total,
(SELECT count(Source.DateAdded ) FROM Source
WHERE Int(Date()-Source.DateAdded) BETWEEN 20 AND 44) AS BTWN_20_44,
(SELECT count(Source.DateAdded ) FROM Source
WHERE Int(Date()-Source.DateAdded) BETWEEN 45 AND 60) AS BTWN_45_60,
(SELECT count(Source.DateAdded ) FROM Source
WHERE Int(Date()-Source.DateAdded) BETWEEN 61 AND 90) AS BTWN_61_90,
(SELECT count(Source.DateAdded ) FROM Source
WHERE Int(Date()-Source.DateAdded) BETWEEN 91 AND 180) AS BTWN_91_180,
(SELECT count(Source.DateAdded ) FROM Source
WHERE Int(Date()-Source.DateAdded) > 180) AS GT_180
FROM Source
GROUP BY Source.ItemName;
除非你试图计算每个项目名称的数量......在这种情况下,它有点棘手。
答案 3 :(得分:0)
将Count
语句替换为
Sum(Iif(DateDiff("d",DateAdded,Date())>=91,Iif(DateDiff("d",DateAdded,Date())<=180,'1','0'),'0')) AS BTWN_91_180,
我不是嵌套Iif
的粉丝,但看起来并没有任何方法,因为DateDiff
和BETWEEN...AND
的播放效果不佳。< / p>
要修剪ItemName
s而不添加任何日期,查询块必须包含在更大的查询中,因为无法从查询内部对计算字段进行检查。最终结果是这个查询:
SELECT *
FROM
(
SELECT DISTINCT Source.ItemName AS InvestmentManager,
Sum(Iif(DateDiff("d",DateAdded,Date())>=20,Iif(DateDiff("d",DateAdded,Date())<=44,'1','0'),'0')) AS BTWN_20_44,
Sum(Iif(DateDiff("d",DateAdded,Date())>=45,Iif(DateDiff("d",DateAdded,Date())<=60,'1','0'),'0')) AS BTWN_45_60,
Sum(Iif(DateDiff("d",DateAdded,Date())>=61,Iif(DateDiff("d",DateAdded,Date())<=90,'1','0'),'0')) AS BTWN_61_90,
Sum(Iif(DateDiff("d",DateAdded,Date())>=91,Iif(DateDiff("d",DateAdded,Date())<=180,'1','0'),'0')) AS BTWN_91_180,
Sum(Iif(DateDiff("d",DateAdded,Date())>180,'1','0')) AS GT_180,
Sum(Iif(DateDiff("d",DateAdded,Date())>=20,'1','0')) AS Total
FROM Source
WHERE CompleteState='FAILED'
GROUP BY ItemName
)
WHERE Total > 0;