SQL:是否可以对 INTERVAL 类型的字段进行 SUM() 计算?
我正在尝试对 INTERVAL 进行求和。例如
SELECT SUM(TIMESTAMP1 - TIMESTAMP2) FROM DUAL
是否可以编写一个同时适用于 Oracle 和 SQL Server 的查询?如果是这样,怎么办?
编辑:将日期更改为间隔
I am trying to sum INTERVAL. E.g.
SELECT SUM(TIMESTAMP1 - TIMESTAMP2) FROM DUAL
Is it possible to write a query that would work both on Oracle and SQL Server? If so, how?
Edit: changed DATE to INTERVAL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
恐怕您将无法找到同时适用于 Oracle 和 MSSQL 的解决方案。日期运算在不同版本的 DBMS 上有很大不同。
无论如何,在 Oracle 中我们可以在简单的算术中使用日期。我们有一个函数 NUMTODSINTERVAL ,它将数字转换为一天到秒的间隔。那么让我们把它们放在一起。
简单的测试数据,两行包含大约相隔 12 个小时的日期对:
用于查找经过时间总和的简单 SQL 查询:
仅仅一天多,这是我们所期望的。
使用 TIMESTAMP 列有点费力,但我们仍然可以使用相同的技巧。
在下面的示例中。 T42T 与 T42 相同,只是列的数据类型是 TIMESTAMP 而不是 DATE。该查询提取 DS INTERVAL 的各个组成部分并将它们转换为秒,然后将其求和并转换回 INTERVAL:
至少这个结果以秒为单位!
I'm afraid you're going to be out of luck with a solution which works in both Oracle and MSSQL. Date arithmetic is something which is very different on the various flavours of DBMS.
Anyway, in Oracle we can use dates in straightforward arithmetic. And we have a function NUMTODSINTERVAL which turns a number into a DAY TO SECOND INTERVAL. So let's put them together.
Simple test data, two rows with pairs of dates rough twelve hours apart:
Simple SQL query to find the sum of elapsed time:
Just over a day, which is what we would expect.
Working with TIMESTAMP columns is a little more labourious, but we can still work the same trick.
In the following sample. T42T is the same as T42 only the columns have TIMESTAMP rather than DATE for their datatype. The query extracts the various components of the DS INTERVAL and converts them into seconds, which are then summed and converted back into an INTERVAL:
At least this result is in round seconds!
好吧,经过一番努力,在 stackoverflowers 答案的帮助下,我找到了适合我需求的解决方案。
这会返回一个浮点数(这对我来说完全没问题),代表 Oracle ant SQL Server 上的天数。
我向两个 DATE 添加零的原因是因为在我的例子中,Oracle DB 上的日期列是 TIMESTAMP 类型,而 SQL Server 上的日期列是 DATETIME 类型(这显然很奇怪)。因此,在 Oracle 上向 TIMESTAMP 添加零就像转换为日期一样,并且对 SQL Server DATETIME 类型没有任何影响。
谢谢你们!你真的很有帮助。
Ok, after a bit of hell, with the help of the stackoverflowers' answers I've found the solution that fits my needs.
This returns a float (which is totally fine for me) that represents days both on Oracle ant SQL Server.
The reason I added zero to both DATEs is because in my case date columns on Oracle DB are of TIMESTAMP type and on SQL Server are of DATETIME type (which is obviously weird). So adding zero to TIMESTAMP on Oracle works just like casting to date and it does not have any effect on SQL Server DATETIME type.
Thank you guys! You were really helpful.
您无法对两个日期时间求和。这没有意义 - 即 15:00:00 加 23:59:00 等于什么?第二天某个时间?但是
您可以使用 SQL Server 中的 Dateadd() 等函数来添加时间增量。
You can't sum two datetimes. It wouldn't make sense - i.e. what does 15:00:00 plus 23:59:00 equal? Some time the next day? etc
But you can add a time increment by using a function like Dateadd() in SQL Server.
在 SQL Server 中,只要您的各个时间跨度都小于 24 小时,您就可以执行类似
Gives 的
操作,如果您想处理大于 24 小时的时间跨度,我认为您需要查看 CLR 集成和 TimeSpan 结构。绝对不便携!
编辑: SQL Server 2008 有一个 DateTimeOffset< /a> 数据类型可能有帮助,但不允许进行 SUM 计算或转换为浮点数
In SQL Server as long as your individual timespans are all less than 24 hours you can do something like
Gives
If you wanted to handle timespans greater than 24 hours I think you'd need to look at CLR integration and the TimeSpan structure. Definitely not portable!
Edit: SQL Server 2008 has a DateTimeOffset datatype that might help but that doesn't allow either
SUM
ming or being cast to float我也不认为这是可能的。使用根据您的喜好计算日期值的自定义解决方案。
I also do not think this is possible. Go with custom solutions that calculates the date value according to your preferences.
您还可以使用这个:
You can also use this:
您可以编写自己的聚合函数:-)。请仔细阅读 http://docs.oracle.com/cd/B19306_01/appdev.102/b14289/dciaggfns.htm
您必须通过模板创建对象类型及其主体,然后使用下一个聚合函数这个对象:
最后,检查你的函数:
最后,如果你愿意,你可以创建 SUM 函数。
You Can write you own aggregate function :-). Please read carefully http://docs.oracle.com/cd/B19306_01/appdev.102/b14289/dciaggfns.htm
You must create object type and its body by template, and next aggregate function what using this object:
Last, check your function:
Finally You can create SUM function, if you want.
Oracle Database 23c 将
sum
和avg
函数扩展到 支持间隔类型:Oracle Database 23c has extended the
sum
andavg
functions to support interval types: