- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
TL;WR:当有些月份没有记录(所以应该为0)时,如何查询月总和的平均值?
我的 children 每天都在报告他们做家务的时间(在 PostgreSQL 数据库中)。我的数据集看起来像这样:
date,user,duration
2020-01-01,Alice,120
2020-01-02,Bob,30
2020-01-03,Charlie,10
2020-01-23,Charlie,10
2020-02-03,Charlie,10
2020-02-23,Charlie,10
2020-03-02,Bob,30
2020-03-03,Charlie,10
2020-03-23,Charlie,10
我想知道他们平均每个月做多少。具体来说,我想要的结果是:
=(120+0+0)÷3
=(30+0+30)÷3
=([10+10]+[10+10]+[10+10])÷3
在某些月份,我没有某些用户的记录(例如,2 月和 3 月的 Alice)。因此,运行以下嵌套查询不会返回我想要的结果;事实上,这并没有考虑到因为没有这几个月的记录,所以 Alice 在 2 月和 3 月的贡献应该为 0(这里的平均值被错误地计算为 120)。
-- this does not work
SELECT
"user",
round(avg(monthly_duration)) as avg_monthly_sum
FROM (
SELECT
date_trunc('month', date),
"user",
sum(duration) as monthly_duration
FROM
public.chores_record
GROUP BY
date_trunc('month', date),
"user"
) AS monthly_sum
GROUP BY
"user"
;
-- Doesn't return what I want:
--
-- "unique_user","avg_monthly_sum"
-- "Alice",120
-- "Bob",30
-- "Charlie",20
因此,我构建了一个相当繁琐的查询如下:
SELECT
unique_user,
round(avg(COALESCE(monthly_duration, 0))) -- COALESCE transforms 'null' into 0
FROM (
-- monthly duration with 'null' if no record for that user×month
SELECT
month_user_combinations.month,
month_user_combinations.unique_user,
monthly_duration.monthly_duration
FROM
(
(
-- all months×users combinations
SELECT
month,
unique_user
FROM (
(
-- list of unique months
SELECT DISTINCT
date_trunc('month', date) as month
FROM
public.chores_record
) AS unique_months
CROSS JOIN
(
-- list of unique users
SELECT DISTINCT
"user" as "unique_user"
FROM
public.chores_record
) AS unique_users
)
) AS month_user_combinations
LEFT OUTER JOIN
(
-- monthly duration for existing month×user combination only
SELECT
date_trunc('month', date) as month,
"user",
sum(duration) as monthly_duration
FROM
public.chores_record
GROUP BY
date_trunc('month', date),
"user"
) AS monthly_duration
ON (
month_user_combinations.month = monthly_duration.month
AND
month_user_combinations.unique_user = monthly_duration.user
)
)
) AS monthly_duration_for_all_combinations
GROUP BY
unique_user
;
这个查询有效,但是非常庞大。
如何比上面更优雅的查询月总和的平均值,同时考虑“无记录⇒月总和=0”?
注意:可以安全地假设我想计算只有至少一个记录的月份的平均值(即这里不考虑 12 月或 4 月是正常的。)
CREATE TABLE public.chores_record
(
date date NOT NULL,
"user" text NOT NULL,
duration integer NOT NULL,
PRIMARY KEY (date, "user")
);
INSERT INTO
public.chores_record(date, "user", duration)
VALUES
('2020-01-01','Alice',120),
('2020-01-02','Bob',30),
('2020-01-03','Charlie',10),
('2020-01-23','Charlie',10),
('2020-02-03','Charlie',10),
('2020-02-23','Charlie',10),
('2020-03-02','Bob',30),
('2020-03-03','Charlie',10),
('2020-03-23','Charlie',10)
;
最佳答案
您可以使用 CTE 构建日历表:
-- EXPLAIN
WITH cal AS ( -- The unique months
SELECT DISTINCT date_trunc('mon', zdate) AS tick
FROM chores_record
)
, cnt AS ( -- the number of months (a scalar)
SELECT COUNT(*) AS nmonth
FROM cal
)
SELECT
x.zuser
, SUM(x.duration) AS tot_duration
, SUM(x.duration) / SUM(c.nmonth) AS Averarage_month -- this is ugly ...
FROM cal t
JOIN cnt c ON true -- This is ugly
LEFT JOIN chores_record x ON date_trunc('mon', x.zdate) = t.tick
GROUP BY x.zuser
;
关于SQL:当有些月份没有记录时,如何查询每月总和的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64843225/
我正在用 javascript 制作日历,我不想在 中显示当前日期、日期名称和月份名称它在我的函数内 Kalender()但由于某种原因,如果我执行函数 volgende() (下个月)它更改 中
有一个输入标签应用了数据掩码。 这允许输入数据,例如 02/12/2014 或 13/05/2014。但不接受 1/1/2013 或 13/5/2014。请帮忙!!! 最佳答案 根据jQuery
我正在尝试创建一个函数,它将接受三个输入变量(开始日期、结束日期、粒度),并将输出一个包含所有日期的数组。输出将根据输入以每日或每月为粒度。例如,如果我查看 2015 年 1 月 - 2015 年 2
我有这段代码可以从数据库中获取一个字段: $end_date=$row1['end_date']; 如果我打印它,它会给我这样的信息:25-09-2012我需要的是获取月份值、年份和日期。类似于: $
我有一个事件表,其中 X 为频率(每 X 个月一次),第一个开始日期和结束日期如下: 如何复制每一行并将其粘贴到新工作表中,并根据 X 和月份日期的增量为每一行添加附加行,如下所示: 最佳答案 这里是
我将以下格式的日期存储在Mailed_Date列中 Mon, 09/20/10 01:04 PM 我使用了一个serde(csv-serde-1.1.2-0.11.0-all.jar)从csv文件中获
我想根据日期和月份值对数组进行排序。我正在使用此代码 NSSortDescriptor *descriptor=[[NSSortDescriptor alloc]initWithKey:@"self
我需要当前年份,月份和日期为3个不同的变量。下面的代码给出了日期时间 val now = Calendar.getInstance().getTime() 2016年9月29日星期四18:27:38
我无法获得正确的查询结果 我正在查询获取在相应月份注册了多少用户。到目前为止,我提出了以下查询,它允许我获得该结果,但没有显示没有用户注册的月份 SELECT YEAR(c.created_at) a
在 Programming in the Key of C# 中,作者给出了一个示例(附源代码),说明如何将日期(年、月、日 -- 数字)打包为 32 位整数。在示例中,作者将信息打包如下: int
在表中,我有一列名为“service_time”的列,它是服务的开始日期,另一列“times_year”是每年应完成服务的次数。 我遇到的问题是如何在 -> (thisMonth == service
使用:Python 3.6, Pandas 0.22 我有一个 .csv 文件,我需要从中获取基于月份和位置的平均值。这是数据中的一行,还有更多具有多个位置和日期的行: 名称日期雪 大急流城杰拉尔德福
我想循环遍历给定开始时间以来的月份,并打印第一天和最后一天。我可以手动跟踪它是哪个月份和年份,并使用 calendar.monthrange(year, Month) 来获取天数......但这是最好
我正在做试卷的最后一题,但我已经迷失在算法的创建过程中。我的定义图看起来不错,但我就是无法确定计算月份方面的顺序。 题目如下: A file of transaction records includ
我正在获取系统当前日期并尝试在 TextView 中显示它。 尝试下面的代码后 private OnClickListener listener1 = new OnClickListener() {
我正在使用以下格式来格式化 DateTime: DateTime CusDate = dateTimePicker1.Value; string Date = CusDate.ToString("dd
废话不多少,上代码 复制代码 代码如下: // 获取指定日期所在星期的开始时间与结束时间 function getWeekRange($date){ &nb
今天我运行了自动化测试,并想知道为什么我收到了有关某些特定日期内容的错误。 事实证明,设置固定的 UTC 月份不再有效。但昨天确实如此。据我所知,没有任何变化。 我尝试运行以下代码 var d = n
我有一个包含两个日期字段的 Grails 域 Date updated Date created 我想根据月、小时或年的更新时间来计算行数。我怎样才能做到这一点。互联网上显示的方法不起作用。我正在使用
是否有任何内置函数可用于数据框对象以生成类 Date 时间序列上的变量以创建星期几、月份、年份、年份中的星期,等在 R 中? 基础包中的weekdays、months、quarters函数生成文本输出
我是一名优秀的程序员,十分优秀!