如何在SQL Server 2008查询中使用sum() [英] How can I use sum() in SQL server 2008 query

查看:347
本文介绍了如何在SQL Server 2008查询中使用sum()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

假设我们有两个时间点像

TimeIn = 2:18 PM&& TimeOut = 2:22 PM所以TimeSpent是4分钟

TimeIn = 2:49PM&& TimeOut = 2:50 PM所以TimeSpent是1分钟

我想添加这个TImeSpent答案,如5,

我只能得到它。但根据我的查询怎么能得到它。



我收到选择列表之类的错误,因为它不包含在聚合函数或GROUP BY子句中。



我尝试过:



Hello,
suppose we have two timespent like
TimeIn=2:18PM && TimeOut=2:22PM so TimeSpent is 4 minutes
TimeIn=2:49PM && TimeOut=2:50PM so TimeSpent is 1 minutes
I want to add this TImeSpent answer like 5,
simply I can get it.but according to my query how can get it.

I am getting error like select list because it is not contained in either an aggregate function or the GROUP BY clause.

What I have tried:

SELECT
CONVERT(varchar(5),ISNULL(DATEADD(minute, DATEDIFF(minute, T.AttendanceTimeIn, ISNULL(T.AttendanceTimeOut,SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30'))), 0),0)- 
(ISNULL(DATEADD(minute, DATEDIFF(minute, T.MealbreakTimeIn, ISNULL(T.MealbreakTimeOut,SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30'))), 0),0) +
ISNULL(DATEADD(minute, sum(DATEDIFF(minute,V.TeabreakTimeIn,ISNULL(V.TeabreakTimeOut,SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')))),0),0) +
ISNULL(DATEADD(minute, DATEDIFF(minute, T.OtherbreakTimeIn, ISNULL(T.OtherbreakTimeOut,SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30'))), 0),0)),114) AS [TimeSpentWork]
FROM TeaBreaks V,DailyTimeTrackers T ;

推荐答案

SUM是一个聚合函数 - 它用于累加列中的所有值,因此您可以使用它来组合销售总额by salesman:

SUM is a aggregate function - it's used to add up all the values in a column, so you would use it to assemble sales totals by salesman:
SELECT SalesName, SUM(SaleValue) FROM MyTable GROUP BY SalesName



你没有用它来尝试和将一部分列添加到同一列的其他部分。



说实话,我不知道SQL应该做什么,或者为什么你使它复杂化。回到您的完整数据,并仔细查看您正在尝试做什么。看看你的其他问题,我认为你需要退后一步,对SQL和数据库进行一般性的快速复习课程,因为你似乎不了解正在发生的任何事情。


You don't use it to try and add up part of a column with other parts of the same column.

And to be honest, I have no idea what that SQL is supposed to do, or why you are making it that complex. Go back to your complete data, and have a close look at exactly what you are trying to do. Looking at your other questions, I think you need to take a step back and do a quick refresher course on SQL and databases in general, because you don't seem to understand any of what is going on.


这篇关于如何在SQL Server 2008查询中使用sum()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆