php - SQL window functions via PHP PDO?
问题描述
I'm trying to get max, average, and latest values from my table of stuff. Two columns exist that are pertinant: created_at
and value
. I'm interested in grouping by day, but needing the latest value prevents me from being able to use group by. So I'm using window functions and I have the SQL that works from the MySql client:
select distinct
DAYOFWEEK(created_at) as dow,
MAX(value) OVER(PARTITION BY dow ORDER BY created_at) as maxv,
AVG(value) OVER(PARTITION BY dow ORDER BY created_at) as avgv,
LAST_VALUE(value) OVER(PARTITION BY dow ORDER BY created_at) as lastv
FROM `insight_follower_counts`;
However, when I try to run this through PHP PDO (actually I'm using Laravel's interface)
$data = DB::select('select distinct ' .
'DAYOFWEEK(created_at) as dow, ' .
'MAX(value) OVER(PARTITION BY dow ORDER BY created_at) as maxv, ' .
'AVG(value) OVER(PARTITION BY dow ORDER BY created_at) as avgv, '.
'LAST_VALUE(value) OVER(PARTITION BY dow ORDER BY created_at) as lastv ' .
'FROM insight_online_followers');
I get the following error:
Doctrine/DBAL/Driver/PDOException with message 'SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause'
Is there a way to do this through PHP PDO? I tried searching but can find nothing related to window functions used via PHP.
解决方案
推荐阅读
- windows - 我们可以使用 Windows 容器进行生产吗
- linux - Yocto Custom Image Recipe 不会将文件安装到最终的 rootfs
- express - 如何将 Oauth 令牌从服务器的回调发送到客户端?
- ios - 为每个 SCNNode 人脸设置不同的图像
- ruby-on-rails - 编译的 Jruby 应用程序实例变量错误
- java - Graphics.Path.Direction - Java 枚举方向问题 - 无法使用 EAST
- flowtype - 无法从 SyntheticMouseEvent 获取属性 offsetX
- ember.js - Puppeteer 无法识别节点
- c# - 使用 canceltoken 取消异步任务
- jenkins - 詹金斯工作自动化