热衷于使用间隔 2 秒的 datetime 分组吗?
我有一个包含数据的表:
id created price amount
2204155 2011-12-15 17:08:11.163 71 54150
2204156 2011-12-15 17:08:11.177 71 54150
2204157 2011-12-15 17:08:11.177 71 54150
2204158 2011-12-15 17:08:11.177 71 54150
我有一个 sql 查询:
select created, price, amount
from table
group by created, price, amount
结果:
created price amount
2011-12-15 17:08:11.163 71 54150
2011-12-15 17:08:11.177 71 54150
我想接收单行。 是否可以在按 2 秒间隙创建的节定义中适应同一组?
谢谢
I have a table with data:
id created price amount
2204155 2011-12-15 17:08:11.163 71 54150
2204156 2011-12-15 17:08:11.177 71 54150
2204157 2011-12-15 17:08:11.177 71 54150
2204158 2011-12-15 17:08:11.177 71 54150
I have a sql query:
select created, price, amount
from table
group by created, price, amount
Result:
created price amount
2011-12-15 17:08:11.163 71 54150
2011-12-15 17:08:11.177 71 54150
I'd like to receive single line.
Is it possible in group by section define created with a gap of 2 seconds to fit in same group?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基础数学告诉我们,如果有一个偶数,则加或减 1 会产生一个奇数。推而广之,任何两个相邻的奇数彼此相差+/-2;例如,3秒和5秒之间的间隔是2秒。
稍微高级一点的数学教给我们模数算术;为了这个答案,您真正需要知道的是
任何数字 % 2
(%
符号通常用于表示 mod 运算符)将返回1 或 0,表示该数字是偶数还是奇数。这意味着我们可以创建一个语句,表示“获取
datetime
”列的第二个值并检查它是偶数还是奇数。如果是偶数,则在datetime
值上加1秒并输出;如果是奇数,则输出未更改的datetime
这将生成一个输出,其中每个
datetime
值都向上舍入到最接近的奇数秒,这意味着GROUP BY
该datetime
值上的 code> 将生成两秒间隔的聚合。示例代码如下:
初始输入:
最终输出:
Basic math teaches us that if you have an even number, adding or subtracting 1 from it produces an odd number. By extension, any two adjacent odd numbers are +/- 2 from each other; for example, the gap between 3 seconds and 5 seconds is 2 seconds.
Slightly more advanced math teaches us about Modular arithmetic; for the sake of this answer, all you really need to know is that
any number % 2
(the%
symbol is often used to represent the mod operator) will return either a 1 or a 0, indicating whether the number is even or odd.What this means is that we can create a statement that says 'Take the second value of a
datetime
column and check to see whether it is even or odd. If it is even, add 1 second to thedatetime
value and output it; if it is odd, output the unchangeddatetime
This produces an output where each
datetime
value is rounded up to the nearest odd second, meaning that aGROUP BY
on thatdatetime
value will produce aggregates for two-second intervals.Sample code follows:
Initial input:
Final output:
不确定如何在 SQL Server 中具体执行此操作,但在 MySQL 中,您不仅可以使用
DATE_FORMAT()
来SELECT
特定格式的日期,还可以对日期进行分组。就您而言,我想您希望将日期格式化为2011-12-15 17:08
之类的结构(例如YYYY-MM-DD HH:MM
)它将按分钟分组。但不知道如何将其分解为两秒钟。Not sure how to do this specifically in SQL Server, but in MySQL you can use
DATE_FORMAT()
not only toSELECT
a date in a specific format, but also to group on. In your case I guess you'd want to format your dates in a structure like2011-12-15 17:08
(egYYYY-MM-DD HH:MM
) which would group by minute. Not sure how you could break it down into two seconds though.