好的,我有这个查询正在创建一个新列(总计),它是StartDateTime和EndDateTime之间的时间跨度(以分钟为单位)。我真正想做的不是返回这几个数字,而只返回这几个数字的总和。 (基本上查询将返回一个数字,即“总计”列的总和)。请参阅下面的快照,我也将查询放在这里供您修改。谢谢!
SELECT ID, StartDateTime, EndDateTime, DATEDIFF(mi, StartDateTime, EndDateTime) AS Total
FROM tEvent
WHERE (EventDefinitionID = '1427' OR
EventDefinitionID = '1428' OR
EventDefinitionID = '1429' OR
EventDefinitionID = '1430' OR
EventDefinitionID = '1432' OR
EventDefinitionID = '1434' OR
EventDefinitionID = '1435' OR
EventDefinitionID = '1436' OR
EventDefinitionID = '1437' OR
EventDefinitionID = '1438' OR
EventDefinitionID = '1439' OR
EventDefinitionID = '1440' OR
EventDefinitionID = '1441' OR
EventDefinitionID = '1442' OR
EventDefinitionID = '1443' OR
EventDefinitionID = '1444' OR
EventDefinitionID = '1445' OR
EventDefinitionID = '1446') AND (EndDateTime IS NOT NULL) AND (DAY(StartDateTime) = DAY(GETDATE()))
ORDER BY StartDateTime DESC
答案 0 :(得分:3)
如果我正确理解了这个问题,只需将您的查询更改为以下内容,即只计算一个数字,即所有总数的总计。稍微简化并重新格式化,这是查询:
SELECT SUM(DATEDIFF(mi, StartDateTime, EndDateTime)) AS GrandTotal
FROM tEvent
WHERE (EventDefinitionID IN ('1427','1428','1429','1430','1432','1434',
'1435','1436','1437','1438','1439','1440',
'1441','1442','1443','1444','1445'))
AND EndDateTime IS NOT NULL
AND DAY(StartDateTime) = DAY(GETDATE())
如果您的EventDefinitionID是数字,则可以使用BETWEEN
使查询更具可读性:
SELECT SUM(DATEDIFF(mi, StartDateTime, EndDateTime)) AS GrandTotal
FROM tEvent
WHERE (EventDefinitionID BETWEEN 1427 AND 1430
OR EventDefinitionID = 1432
OR EventDefinitionID BETWEEN 1434 AND 1445)
AND EndDateTime IS NOT NULL
AND DAY(StartDateTime) = DAY(GETDATE())
答案 1 :(得分:1)
试
SELECT SUM(DATEDIFF(mi, StartDateTime, EndDateTime)) AS Total
FROM tEvent
WHERE (EventDefinitionID = '1427' OR
EventDefinitionID = '1428' OR
EventDefinitionID = '1429' OR
EventDefinitionID = '1430' OR
EventDefinitionID = '1432' OR
EventDefinitionID = '1434' OR
EventDefinitionID = '1435' OR
EventDefinitionID = '1436' OR
EventDefinitionID = '1437' OR
EventDefinitionID = '1438' OR
EventDefinitionID = '1439' OR
EventDefinitionID = '1440' OR
EventDefinitionID = '1441' OR
EventDefinitionID = '1442' OR
EventDefinitionID = '1443' OR
EventDefinitionID = '1444' OR
EventDefinitionID = '1445' OR
EventDefinitionID = '1446') AND (EndDateTime IS NOT NULL) AND (DAY(StartDateTime) = DAY(GETDATE()))