sql - PostgreSQL How to get average of sum of certain rows
问题描述
I apologize if the title isn't very apt, but I had a little trouble formulating a concise title. Anyway, I have a table that keeps a log of states a person is in. It looks like:
id, login, state, duration, started_at 1, pdiddy, working, 1200, 2018-05-25 08:30:00 2, pdiddy, lunch, 120, 2018-05-25 9:00:00 3, pdiddy, on_call, 65, 2018-05-25 12:30:00 4, pdiddy, available, 1115, 2018-05-25 12:30:00 5, pdiddy, working, 143, 2018-05-25 12:30:00 6, pdiddy, break1, 150, 2018-05-25 12:30:00 7, pdiddy, working, 2400, 2018-05-25 12:30:00 8, pdiddy, break2, 110, 2018-05-25 12:30:00
I need to get an average for only labor-related durations on a day-by-day basis for each user. So basically I need to add up the durations for everything other than "lunch", "break1", and "break2" for any given day and get the average for it.
I tried to do this like this, but the problem with this is that it doesn't add up the labor-related logs before averaging. I can't figure out how to do that.
SELECT
log.login,
AVG(log.duration) FILTER (WHERE log.state NOT IN ('lunch', 'break1', 'break2')) AS "labor_average"
FROM
log
GROUP BY 1
Obviously I don't expect anybody to just do this for me. I just need to be pointed in the right direction. I'm obviously quite a ways from the solution, so I just need a push in the right direction. Thank you very much in advance!
解决方案
Calculate sum per login and per day first.
Then calculate average per login.
SELECT
login, AVG(SumDuration) AS AvgDuration
FROM
(
SELECT
login, started_at::date, SUM(duration) AS SumDuration
FROM log
WHERE log.state NOT IN ('lunch', 'break1', 'break2')
GROUP BY login, started_at::date
) AS T
GROUP BY login;
推荐阅读
- r - 根据 data.table 中附加列中的条件从同一列中对多行进行子集
- javascript - 需要帮助使用 express-handlebars 遍历变量的子项
- java - 错误:在 PytorchDemoApp 中找不到符号
- python - 如何在 numpy 数组中实现高效的增量搜索?
- javascript - 调整标签内容和背景高度
- java - 用于在二维矩阵中查找相同符号簇的 DFS 函数
- xpages - xp:dataContext 变量是只读的吗?
- excel - 代码不会从某些 URL 中提取图像
- c# - 带有脚本标签元素的部分视图违反了内容安全策略
- sql - 根据参数隐藏列 - SSRS