操作数数据类型datetime对sum运算符无效 [英] Operand data type datetime is invalid for sum operator

查看:1387
本文介绍了操作数数据类型datetime对sum运算符无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我在我的桌子上有这个记录



Hi I have this record in my table

DateId  Date                     Id
1       2013-06-01 00:00:00.000  1
2       2013-07-05 00:00:00.000  1
3       2013-08-08 00:00:00.000  1
4       2013-09-10 00:00:00.000  1





我想要的是得到总数运行日期字段的总和。



这是我正在使用的查询但是我收到此错误<<操作数数据类型datetime对sum运算符无效。>> ;:





what I want is to get the total running sum of the Date field.

This is the query am using but am getting this error<<Operand data type datetime is invalid for sum operator.>>:

SELECT *, (SELECT DateDiff(DD,a.[Date],SUM(b.[Date]))
                       FROM [dbo].[Date] b
                       WHERE b.[DateId] <= a.[DateId]) as DateSum
   FROM   [dbo].[Date] a
   ORDER BY a.[DateId];





有人可以帮助



Can someone help

推荐答案

这是一个非常明智的投诉:想一想。



你得到的是你在1914年2月3日到2012年6月8日添加的东西?

没有任何意义。



所以SQL基本上说我不知道怎么做:我不知道结果会是什么。

我必须同意。你可以通过一起添加日期来得到任何明智的结果(不像减去它们,但即便如此,如果你试图从一个日期中减去两个日期,你会得到什么?)



重新思考你想要做的事情:它在现实世界中没有任何意义!
It's making a very sensible complaint here: think about it.

What do you get is you add 3rd February 1914 to 8 June 2012?
Nothing that makes any sense.

So SQL is basically saying "I don't know how to do that: I have no idea what the result will be".
And I have to agree with it. There is nothing sensible you can get from adding dates together (unlike subtracting them, but even then, what the heck would you get if you try to subtract two dates from one date?)

Rethink what you are trying to do: it doesn't make any sense in the real world either!


这篇关于操作数数据类型datetime对sum运算符无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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