选择一个间隔的几个平均值(select several averages of an interval)
所以我的同事正在查看一个可以描述为这样的模式:
+--------------------+-----------+ | DATETIME timestamp | INT value | +--------------------+-----------+
每隔5分钟输入一行,该值为该时刻的值。
这是它变得棘手的地方。 他希望在7天的间隔内获得每8小时的平均值。
当然,我可以想到涉及一些客户端代码的解决方案,我们想知道是否有可能在SQL中做更多。
所以从本质上说,他想:
SELECT timestamp, value FROM table WHERE timestamp >= NOW() - INTERVAL 7 DAYS AND timestamp <= NOW();
然后将其分解为8小时块,并平均每个块的内容。 (每个块应该有12行,每天应该有3个块)。
So my coworker is looking at a schema which could be described as something like this:
+--------------------+-----------+ | DATETIME timestamp | INT value | +--------------------+-----------+
Every 5 minutes a row is entered with the value for that moment.
Here's where it gets tricky. He wants to get the average of every 8 hour period within a 7 day interval.
Certainly, I can think of solutions which involve some client side code, we were wondering if it was possible to do more in SQL.
So in essence, he wants:
SELECT timestamp, value FROM table WHERE timestamp >= NOW() - INTERVAL 7 DAYS AND timestamp <= NOW();
And then breaking that up into 8 hour blocks, and averaging the contents of each block. (each block should have 12 rows, and there should be 3 blocks per day).
原文:https://stackoverflow.com/questions/11743029
最满意答案
精确的条件在
[[Construct]]
内部属性中描述,由new
运算符使用:从ECMA-262第三。 版本规格:
13.2.2
[[Construct]]
当调用
Function
对象F
的[[Construct]]
属性时,将执行以下步骤:
- 创建一个新的本地ECMAScript对象。
- 将
Result(1)
的[[Class]]
属性设置为"Object"
。- 获取
F
的原型属性的值。- 如果
Result(3)
是一个对象,将Result(1)
的[[Prototype]]
属性设置为Result(3)
。- 如果
Result(3)
不是一个对象,则将Result(1)
的[[Prototype]]
属性设置为原始Object
原型对象,如15.2.3.1所述 。- 调用
F
的[[Call]]
属性,提供Result(1)
作为this
值,并将传递给[[Construct]]
的参数列表作为参数值。- 如果
Type (Result(6))
为Object
则返回Result(6)
。- 退货
Result(1)
。查看步骤7和8,只有当
Result(6)
类型Result(6)
从F
构造函数返回的值) 不是 Object时,才会返回新对象。The exact condition is described on the
[[Construct]]
internal property, which is used by thenew
operator:From the ECMA-262 3rd. Edition Specification:
13.2.2
[[Construct]]
When the
[[Construct]]
property for aFunction
objectF
is called, the following steps are taken:
- Create a new native ECMAScript object.
- Set the
[[Class]]
property ofResult(1)
to"Object"
.- Get the value of the prototype property of
F
.- If
Result(3)
is an object, set the[[Prototype]]
property ofResult(1)
toResult(3)
.- If
Result(3)
is not an object, set the[[Prototype]]
property ofResult(1)
to the originalObject
prototype object as described in 15.2.3.1.- Invoke the
[[Call]]
property ofF
, providingResult(1)
as thethis
value and providing the argument list passed into[[Construct]]
as the argument values.- If
Type(Result(6))
isObject
then returnResult(6)
.- Return
Result(1)
.Look at steps 7 and 8, the new object will be returned only if the type of
Result(6)
(the value returned from theF
constructor function) is not an Object.
相关问答
更多-
之前会处理返回值的复制初始化。 来自标准, $6.6.3/3 The return statement [stmt.return] (强调我的) 在返回语句的操作数所建立的完整表达式的末尾销毁临时对象之前,对返回实体的复制初始化进行排序,然后在返回语句的局部变量(6.6)封闭返回语句块 。 The copy-initialization of the returned value will be processed before. From the standard, [stmt.return]/3 (em ...
-
当从函数返回值以避免复制时使用std :: move()(Using std::move() when returning a value from a function to avoid to copy)[2021-12-10]
否。只要return语句中的局部变量有资格进行复制检查,它将绑定到一个右值引用,从而return t; 与return std::move(t); 在你的例子中,哪些构造函数有资格。 注意return std::move(t); 防止编译器在return t执行复制检查; 不是,因此后者是首选的风格。 [感谢@Johannes的纠正。]如果复印错误发生,是否使用移动构造的问题成为一个难题。 见标准中的12.8(31,32)。 还要注意,如果T有一个可访问的副本,而是一个删除的移动构造函数,那么return ... -
构造函数实际发生的是,运行时使用编译器生成的类型数据来确定将对象实例存储在内存中(无论是在堆栈还是堆上)需要多少空间。 该空间包括所有成员变量和vtbl。 在分配此空间之后,构造函数被称为实例化和初始化过程的内部部分,以初始化字段的内容。 然后,当构造函数退出时,运行时返回新创建的实例。 所以构造函数不返回一个值的原因是因为它不是直接由你的代码调用的,它是由运行时内存分配和对象初始化代码调用的。 它的返回值(如果在编译到机器代码的时候实际上有一个)对于用户来说是不透明的,因此你不能指定它。 What act ...
-
构造函数不会得到返回值; 他们完全可以实例化课程。 没有重组您已经在做的事情,您可以考虑在此使用例外。 public function __construct ($identifier = NULL) { $this->emailAddress = $identifier; $this->loadUser(); } private function loadUser () { // try to load the user if (/* not able to load user ...
-
精确的条件在[[Construct]]内部属性中描述,由new运算符使用: 从ECMA-262第三。 版本规格: 13.2.2 [[Construct]] 当调用Function对象F的[[Construct]]属性时,将执行以下步骤: 创建一个新的本地ECMAScript对象。 将Result(1)的[[Class]]属性设置为"Object" 。 获取F的原型属性的值。 如果Result(3)是一个对象,将Result(1)的[[Prototype]]属性设置为Result(3) 。 如果Result( ...
-
将@Mocked注释更改为@Capturing ,如下所示: @Capturing Collaborator collaborator; 这可以让我通过测试。 在我看来,这有点巫术魔法,但如果您想要阅读更多内容,请查看JMockit教程中捕获模拟类型的内部实例 。 另请参阅使用JMockit从模拟构造函数返回实际实例 Change the @Mocked annotation to @Capturing, like this: @Capturing Collaborator collaborator; ...
-
如何(以及为什么)可以避免在这些异步方法上返回一个空值?(How(and why) can I avoid returning a void on these async methods?)[2021-07-21]
一旦初始化,构造器就意味着将一个对象带到完全构造的结构中。 另一方面,异步方法和构造函数不能很好地发挥作用,因为构造函数是继承同步的。 解决此问题的方法通常是为类型提供初始化方法,该方法本身是异步的。 现在,你让调用者完全初始化对象。 请注意,这将要求您监视方法的实际初始化。 当你需要规模时,异步闪耀。 如果您不希望这会成为应用程序中的IO瓶颈,那么可以考虑使用同步方法。 一旦构造器完成执行,这会给你实际完全初始化对象的好处。 虽然,我不认为我会通过构造函数来启动对数据库的调用: public async ... -
是“添加”和“res”构造函数吗? 不add()是类X的成员函数并返回X , res是add()一个局部变量,类型为X 构造函数不能返回值 是的这是对的。 Are "add" and "res" constructors? No. add() is a member function of class X and returns X, res is a local variable inside add() with type X. constructors can't return a value Yes ...
-
你的假设非常正确。 对于pass by value,对象在通常传递参数的位置构造,有时在调用函数之前(但在前一个语句之后),并在从函数返回之前被破坏,就好像它们是函数中的局部变量一样。 对于通过引用传递,对象在堆栈上构建(或者通常构造临时的任何地方),并在完整表达式的末尾被破坏。 对于返回值,对象由被调用函数构成,在return语句中(但在调用局部变量的析构函数之前),并由被调用者在完整表达式的末尾进行破坏。 Your assumtions are pretty much correct. For pass ...
-
根据规范:如果调用构造函数返回一个对象,那么这个对象就是new -expression的结果。 如果构造函数未返回对象(但undefined或其他原始值),则结果是新创建的对象。 如果允许原语,那么所有构造函数都必须显式返回一些东西(通常是“ this ”),否则结果将是undefined (因为没有return的函数的结果是undefined )。 这将是一个不必要的麻烦。 此外,有意义的是可以依赖new来始终返回一个对象。 According to the spec: If calling the co ...