1

我需要在 MS Access 中查询列的值与 8 的差异,并且仅当它大于 8 时。

因此,如果我有一列数字 1-10,我想查询所有值与 8 的差的总和。因此,下列的查询结果将是 3. (9-8)+(10-8 )

    SELECT Sum(([time1]-8)+([time2]-8)+([time3]-8)+([time4]-8)+([time5]-8)+([time6]-8)+([time7]-8)+([time8]-8)+([time9]-8)+([time10]-8)+([time11]-8)+([time12]-8)+([time13]-8)+([time14]-8)+([time15]-8)+([time16]-8)+([time17]-8)+([time18]-8)+([time19]-8)+([time20]-8)+([time21]-8)+([time22]-8)) AS Total
FROM tblTimeTracking
WHERE (((Month(([Day])))=Month(Now()))) AND ([time1]>8 AND[time2]>8 AND[time3]>8 AND[time4]>8 AND[time5]>8 AND[time6]>8 AND[time7]>8 AND[time8]>8 AND[time9]>8 AND[time10]>8 AND[time11]>8 AND[time12]>8 AND[time13]>8 AND[time14]>8 AND[time15]>8 AND[time16]>8 AND[time17]>8 AND[time18]>8 AND[time19]>8 AND[time20]>8 AND[time21]>8 AND[time22])  ;   

谢谢,

4

1 回答 1

2

怎么样:

SELECT Sum([Value]-8) As SumOfVal
FROM table
WHERE [Value]>8

编辑重新完整更改原始问题。

不清楚你想要什么

 SELECT Sum(([time1]-8)+([time2]-8) ...
 WHERE [time1]>8 And Time2>8 ...

Time1>8 将排除空值,但如果这不是您正在做的,您将需要考虑:

 Nz([time1],0) + ...

编辑重新评论

就像是:

 SELECT Sum(times) FROM
 (SELECT IIf(Time1>8,Time1-8,Time1) As times FROM Table
 UNION ALL
 SELECT IIf(Time2>8,Time2-8,Time2)  As times FROM Table) As b

As b是别名:Access SQL

UNION / UNION ALL:使用联合查询查看多个查询的统一结果

于 2013-02-06T20:18:28.087 回答