[英]Turning all dates into date ranges
I have a table in my database called sale:我的数据库中有一张表,名为 sale:
SaleID (PK)
Quantity
Date
Time
TotalPrice
TransactionID (FK)
I then have a test table, in the same database with the following structure:然后我有一个测试表,在同一个数据库中,结构如下:
SaleID (PK)
DateFrom
DateTo
TotalPrice
AveragePrice
In the sale, table I have 60,000 dates.在销售中,表 I 有 60,000 个日期。 In my test table I would like the
date from
and date to
be a week.在我的测试表中,我希望起始
date from
和date to
一周。 So for example:例如:
DateFrom - 1st January 2016.
DateTo - 7th January 2016.
AveragePrice - 20,000
It would allow me to view the weekly average sale.它可以让我查看每周的平均销售额。 Is there any way to take all dates from sale in a 7 day range and insert them into test?
有没有办法在 7 天范围内从销售中获取所有日期并将它们插入测试中? I can't think of a query which would make this work.
我想不出可以使这项工作的查询。 I know how to do a query for one week:
我知道如何进行一周的查询:
SELECT * FROM sale where date between 'January 1 2016' and 'January 7 2016'
However I'm unsure how I would insert January 1 2016 as the FromDate and January 7 2016 as the ToDate and do this for every 7 day range.但是,我不确定如何将 2016 年 1 月 1 日作为 FromDate 和 2016 年 1 月 7 日作为 ToDate 插入,并在每 7 天范围内执行此操作。 Is it possible?
是否可以?
This one doesnt work.这个不行。 Last week of 2014 say is week 1
2014 年的最后一周说是第 1 周
SELECT YEAR(`Date`), WEEKOFYEAR(`Date`), AVG(TotalPrice) FROM Sales GROUP BY YEAR(`Date`), WEEKOFYEAR(`Date`)
January 1 2016
will have a week with less than 7 days January 1 2016
将有少于 7 天的一周
If you want 7 day weeks, you can use:如果您想要每周 7 天,您可以使用:
SELECT
SUBDATE(`Date`, WEEKDAY(`Date`)) MondayWeek,
AVG(`TotalPrice`)
FROM Sales
GROUP BY SUBDATE(`Date`, WEEKDAY(`Date`))
I include another query to see how this work我包括另一个查询以查看它是如何工作的
SELECT `Date`, WEEKDAY(`Date`), YEAR(`Date`), WEEKOFYEAR(`Date`), yearWeek(`Date`)
FROM Sales
order by `Date`;
OUTPUT输出
| Date | WEEKDAY(`Date`) | YEAR(`Date`) | WEEKOFYEAR(`Date`) | YEARWEEK(`Date`) |
|------------|-----------------|--------------|--------------------|------------------|
| 2014-12-24 | 2 | 2014 | 52 | 201451 |
| 2014-12-25 | 3 | 2014 | 52 | 201451 |
| 2014-12-26 | 4 | 2014 | 52 | 201451 |
| 2014-12-27 | 5 | 2014 | 52 | 201451 |
| 2014-12-28 | 6 | 2014 | 52 | 201452 |
| 2014-12-29 | 0 | 2014 | <===> 1 | 201452 | ***
| 2014-12-30 | 1 | 2014 | 1 | 201452 |
| 2014-12-31 | 2 | 2014 | 1 | 201452 |
| 2015-01-01 | 3 | 2015 | <===> 1 | 201452 | ***
| 2015-01-02 | 4 | 2015 | 1 | 201452 |
| 2015-01-03 | 5 | 2015 | 1 | 201452 |
| 2015-01-04 | 6 | 2015 | 1 | 201501 | ***
| 2015-01-05 | 0 | 2015 | 2 | 201501 |
| 2015-01-06 | 1 | 2015 | 2 | 201501 |
| 2015-01-07 | 2 | 2015 | 2 | 201501 |
| 2015-01-08 | 3 | 2015 | 2 | 201501 |
| 2015-01-09 | 4 | 2015 | 2 | 201501 |
| 2015-01-10 | 5 | 2015 | 2 | 201501 |
| 2015-01-11 | 6 | 2015 | 2 | 201502 |
| 2015-01-12 | 0 | 2015 | 3 | 201502 |
| 2015-01-13 | 1 | 2015 | 3 | 201502 |
| 2015-01-14 | 2 | 2015 | 3 | 201502 |
| 2015-01-15 | 3 | 2015 | 3 | 201502 |
| 2015-01-16 | 4 | 2015 | 3 | 201502 |
There are many ways to Rome, so are there to solve this problem.去罗马的方法有很多,所以都有解决这个问题的方法。 Best way, in my opinion, introduce a calendar table.
在我看来,最好的方法是引入日历表。
For example:例如:
DateSQL DateText Week Year Month Day Weekday Period YearPeriod MonthPeriod
2014-12-29 29-12-2014 1 2014 12 29 Monday 1 2015 1
2014-12-30 30-12-2014 1 2014 12 30 Tuesday 1 2015 1
2014-12-31 31-12-2014 1 2014 12 31 Wednesday 1 2015 1
2015-01-01 01-01-2015 1 2015 1 1 Thursday 1 2015 1
2015-01-02 02-01-2015 1 2015 1 2 Friday 1 2015 1
2015-01-03 03-01-2015 1 2015 1 3 Saturday 1 2015 1
2015-01-04 04-01-2015 1 2015 1 4 Sunday 1 2015 1
2015-01-05 05-01-2015 2 2015 1 5 Monday 1 2015 1
It can solve a lot of problems in query's for determine several date issues.它可以解决查询中确定多个日期问题的很多问题。 For example: 29-12-2014 belongs to week 1 of year 2015. Or: 31-12-2015 belongs to week 53 of year 2015, but (in our case) to period 13 (and not 14, in our 4-weekly period).
例如:29-12-2014 属于 2015 年的第 1 周。或者: 31-12-2015 属于 2015 年的第 53 周,但(在我们的例子中)到第 13 周(而不是 14,在我们的 4-weekly时期)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.