简体   繁体   English

如何使用SQL按周获取滚动数据集

[英]How to get a rolling data set by week with sql

I had a sql query I would run that would get a rolling sum (or moving window) data set. 我有一个要运行的sql查询,它将获得滚动总和(或移动窗口)数据集。 I would run this query for every 7 days, increase the interval number by 7 (28 in example below) until I reached the start of the data. 我将每7天运行一次此查询,将间隔号增加7(在下面的示例中为28),直到到达数据开始为止。 It would give me the data split by week so I can loop through it on the view to create a weekly graph. 它将给我按周划分的数据,因此我可以在视图上循环遍历以创建每周图。

  SELECT *
  FROM `table`
  WHERE `row_date` >= DATE_SUB(NOW(), INTERVAL 28 DAY)
  AND `row_date` <= DATE_SUB(NOW(), INTERVAL 28 DAY)

This is of course very slow once you have several weeks worth of data. 一旦拥有数周的数据,这当然会非常慢。 I wanted to replace it with a single query. 我想用一个查询替换它。 I came up with this. 我想到了这个。

  SELECT *
  CONCAT(YEAR(row_date), '/', WEEK(row_date)) as week_date
  FROM `table`
  GROUP BY week_date
  ORDER BY row_date DESC

It appeared mostly accurate, except I noticed the current week and the last week of 2015 was much lower than usual. 它似乎大部分都是准确的,但我注意到本周和2015年的最后一周比平常低得多。 That's because this query gets a week starting on Sunday (or Monday?) meaning that it resets weekly. 这是因为此查询从星期日(或星期一?)开始需要一个星期,这意味着它每周重置一次。

Here's a data set of employees that you can use to demonstrate the behavior. 这是一组员工数据,可用于演示行为。

CREATE TABLE employees (
    id          INT             NOT NULL,
    first_name  VARCHAR(14)     NOT NULL,
    last_name   VARCHAR(16)     NOT NULL,
    row_date    DATE            NOT NULL,
    PRIMARY KEY (id)
);

INSERT INTO `employees` VALUES
(1,'Bezalel','Simmel','2016-12-25'),
(2,'Bezalel','Simmel','2016-12-31'),
(3,'Bezalel','Simmel','2017-01-01'),
(4,'Bezalel','Simmel','2017-01-05')

This data will return the last 3 rows on the same data point on the old query (last 7 days) assuming you run it today 2017-01-06, but only the last 2 rows on the same data point on the new query (Sunday to Saturday). 假设您今天在2017年1月6日运行此数据,它将返回旧查询(过去7天)的同一数据点的最后3行,但新查询(星期日)仅返回同一数据点的最后2行到星期六)。

For more information on what I mean by rolling or moving window, see this English stack exchange link. 有关滚动或移动窗口的含义的更多信息,请参见此英语堆栈交换链接。

https://english.stackexchange.com/questions/362791/word-for-graph-that-counts-backwards-vs-graph-that-counts-forwards https://english.stackexchange.com/questions/362791/word-for-graph-that-c​​ounts-backwards-vs-graph-that-c​​ounts-forwards

How can I write a query in MySQL that will bring me rolling data, where the last data point is the last 7 days of data, the previous point is the previous 7 days, and so on? 如何在MySQL中编写查询,以获取滚动数据,最后一个数据点是最近7天的数据,前一个数据点是前7天的数据,依此类推?

I've had to interpret your question a lot so this answer might be unsuitable. 我不得不对您的问题进行很多解释,因此此答案可能不合适。 It sounds like you are trying to get a graph showing data historically grouped into 7-day periods. 听起来好像您正在尝试获取一个图表,该图表显示了过去7天的历史数据分组。 Your current attempt does this by grouping on calendar week instead of by 7-day period leading to inconsistent size of periods. 您当前的尝试是按日历周而不是按7天时间段进行分组,从而导致时间段大小不一致。

So using a modification of your dataset on sql fiddle ( http://sqlfiddle.com/#!9/90f1f2 ) I have come up with this 因此,在sql小提琴( http://sqlfiddle.com/#!9/90f1f2 )上使用对数据集的修改

  SELECT 
    -- Figure out how many periods of 7 days ago this record applies to
    FLOOR( DATEDIFF( CURRENT_DATE , row_date ) / 7 ) AS weeks_ago,
    -- Count the number of ids in this group
    COUNT( DISTINCT id ) AS number_in_week,
    -- Because this is grouped, make sure to have some consistency on what we select instead of leaving it to chance
    MIN( row_date ) AS min_date_in_week_in_dataset 
  FROM `sample_data`
  -- Groups by weeks ago because that's what you are interested in
  GROUP BY weeks_ago
  ORDER BY 
    min_date_in_week_in_dataset DESC;

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

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