Sql:日期的平均值

发布于 2024-10-21 19:28:18 字数 962 浏览 4 评论 0原文

我必须编写一个查询来计算每个客户购物之间的平均天数(不使用子查询)。

create table data {
    customer varchar(20) not null,
    bought date          not null,
    primary key (customer,bought)
}

例如,

insert into data (customer,bought)
values 
(‘John Smith’,   date ‘2011-02-01’),
(‘Alice Cooper’, date ‘2011-02-01’),
(‘Bob Baker’,    date ‘2011-02-01’),
(‘John Smith’,   date ‘2011-02-02’),
(‘Bob Baker’,    date ‘2011-02-02’),
(‘Bob Baker’,    date ‘2011-02-03’),
(‘Bob Baker’,    date ‘2011-02-04’),
(‘Bob Baker’,    date ‘2011-02-05’),
(‘Bob Baker’,    date ‘2011-02-06’),
(‘Bob Baker’,    date ‘2011-02-07’),
(‘John Smith’,   date ‘2011-02-07’),
(‘Alice Cooper’, date ‘2011-02-08’);

应该返回 John Smith 等了 1 天,然后等了 5 天,所以他的平均值是 3 天。 Alice Cooper(!) 等了 7 天,所以她 平均是 7。鲍勃·贝克是一名每日跑步者,所以他的平均是 1。

我做了这样的事情:

select distinct customer, avg (bought) as average from data;

但它不起作用。

任何帮助将不胜感激。

I have to write a query to calculate the average number days between the shopping for each customer (without using subqueries).

create table data {
    customer varchar(20) not null,
    bought date          not null,
    primary key (customer,bought)
}

For example,

insert into data (customer,bought)
values 
(‘John Smith’,   date ‘2011-02-01’),
(‘Alice Cooper’, date ‘2011-02-01’),
(‘Bob Baker’,    date ‘2011-02-01’),
(‘John Smith’,   date ‘2011-02-02’),
(‘Bob Baker’,    date ‘2011-02-02’),
(‘Bob Baker’,    date ‘2011-02-03’),
(‘Bob Baker’,    date ‘2011-02-04’),
(‘Bob Baker’,    date ‘2011-02-05’),
(‘Bob Baker’,    date ‘2011-02-06’),
(‘Bob Baker’,    date ‘2011-02-07’),
(‘John Smith’,   date ‘2011-02-07’),
(‘Alice Cooper’, date ‘2011-02-08’);

should return that John Smith waited 1 day then 5 days, so his average is 3 days. Alice Cooper(!) waited 7 days so her
average is 7. Bob Baker is a daily runner so his average is 1.

I have done something like this:

select distinct customer, avg (bought) as average from data;

but it doesn't work.

Any help will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

盛夏已如深秋| 2024-10-28 19:28:18

您必须以纪元秒为单位转换时间戳才能使用 avg 聚合函数:

SELECT
    customer,
    timestamp without time zone '1970-01-01' + cast(
       avg(EXTRACT(EPOCH FROM bought::timestamp)
    )::text as interval) 
FROM data
GROUP BY customer;

You have to convert your timestamp in epoch seconds to use avg aggregate function :

SELECT
    customer,
    timestamp without time zone '1970-01-01' + cast(
       avg(EXTRACT(EPOCH FROM bought::timestamp)
    )::text as interval) 
FROM data
GROUP BY customer;
谈情不如逗狗 2024-10-28 19:28:18

PostgreSQL 版本的链接答案

select customer, (max(bought) - min(bought)) / (count(bought)-1)
from data
group by customer;

PostgreSQL version of the linked answer

select customer, (max(bought) - min(bought)) / (count(bought)-1)
from data
group by customer;
夜灵血窟げ 2024-10-28 19:28:18

这在类似的情况下对我有用:

SELECT customer, avg(AGE(now(),bought)) as waited
FROM data 
GROUP BY customer

This worked for me in similar situation:

SELECT customer, avg(AGE(now(),bought)) as waited
FROM data 
GROUP BY customer
葵雨 2024-10-28 19:28:18

选择最大-最小/计数不是平均值。尝试:

 timestamp 'epoch' + avg(date_part(epoch, date_column))  * interval '1 second' 

selecting max-min/count is NOT the average. try:

 timestamp 'epoch' + avg(date_part(epoch, date_column))  * interval '1 second' 
淡淡の花香 2024-10-28 19:28:18

您可能想要使用 group by 语句

select 
   customer, 
   datediff(D, min(bought), max(bought)) / count(bought) as average
from data
group by customer

每当选择列表中有一个聚合函数时,您必须对不属于聚合成员的其他字段使用分组。

这是在 SQL Server 上测试的,语法可能与我没有使用的 Postgresql 不同。

You might want to use a group by statement

select 
   customer, 
   datediff(D, min(bought), max(bought)) / count(bought) as average
from data
group by customer

Whenever you have an aggregate function in the select list, you have to use grouping on the other fields that are not members of the aggregation.

This was tested on SQL Server and the syntax may differ to Postgresql, which I am not using.

别理我 2024-10-28 19:28:18
SELECT customer, AVG(bought) AS average FROM data GROUP BY customer
SELECT customer, AVG(bought) AS average FROM data GROUP BY customer
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文