简体   繁体   中英

Turning all dates into date ranges

I have a table in my database called 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. In my test table I would like the date from and date to be a week. 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? 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. Is it possible?

This one doesnt work. Last week of 2014 say is week 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

If you want 7 day weeks, you can use:

SQL Fiddle Demo

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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