简体   繁体   English

比较以前的平均值和当前的平均值

[英]Comparing Previous Averages With Current Average

I have a table like the following: 我有一个如下表:

Date | Person | Amount_Spent
42500  A        100 
42499  A        90 
42498  A        95

I would like a query that can take the current date (stored as excel dates) and show people where the difference between the amount spent on a given date (42500 in this case), ie 100 and the average of the last 3 days (ie 42500 >= x >= 42498) for example, ie 95 is greater than a certain amount, ie 2. 我想要一个可以获取当前日期(存储为excel日期)的查询,并向人们显示在给定日期(本例中为42500)(即100)和最近3天的平均值(即,例如42500> = x> = 42498),即95大于某个值,即2。

In this example the output should be: 在此示例中,输出应为:

Person
A   

Please ensure that the solution is able to cope with the fact that there will be multiple different people, some people won't spend every day also, so the query should only average the values in the average window, I guess it should include GROUP BY as a way of grouping people together. 请确保该解决方案能够应对以下事实:会有多个不同的人,有些人也不会每天花费,因此查询应仅对平均窗口中的值求平均,我猜它应该包括GROUP BY作为将人们分组在一起的一种方式。

Thanks! 谢谢!

Let's break this down into two pieces: 让我们将其分为两部分:

  1. Calculate the AVG on a given date: 计算给定日期的AVG:

    SELECT AVG(Amount_Spent) FROM table_name WHERE DATE IN (42500, 42499, 42498); 从表名WHERE DATE IN(42500,42499,42498)中选择AVG(Amount_Spent);

  2. Retrieve all Person matching the criteria: 检索所有符合条件的人员:

    SELECT Person FROM table_name WHERE DATE IN (42500, 42499, 42498) AND Amount_Spent >= (SELECT AVG(Amount_Spent) FROM table_name WHERE DATE IN (42500, 42499, 42498)); 从table_name的WHERE DATE IN(42500、42499、42498)和Amount_Spent中选择SELECT> =(从table_name的WHERE DATE IN(42500、42499、42498)中选择AVG(Amount_Spent)

This assumes the dates are stored as integers and that you'd like the average computed on a subset of dates. 假设日期存储为整数,并且您希望对日期的子集计算平均值。 Only Persons who spent an amount greater than the average on those dates are retrieved. 仅检索在那些日期上花费大于平均金额的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM