只有部分答案:
您仍然需要将原始数据输入数据库,并将计算的补货日期传输到日历中。
SELECT "Description", DATEDIFF( 'dd', MIN( "Posting Date" ), MAX( "Posting Date" ) ) / COUNT( * ) AS "Days between purchase", SUM( "Amount" ) / COUNT( * ) AS "Average Price", DATEDIFF( 'dd', MIN( "Posting Date" ), MAX( "Posting Date" ) ) /count(*) as "Average Days Item Lasts", DATEDIFF('dd',MAX( "Posting Date" ),NOW()) as "Last Purchase # Days ago" FROM "credit" GROUP BY "Description"
我在 LibreOffice 中得到了这个工作。不幸的是,Libreoffice 不支持 date_add,所以我不能给你一个重新订购的日期。
这是mysql的更好版本,经过测试
SELECT Description, DATEDIFF( MAX( `Posting Date` ), MIN( `Posting Date` ) ) / COUNT( * ) AS `Order every # Days`, SUM( `Amount` ) / COUNT( * ) AS `Average Price`, MAX( `Posting Date` ) as `Last Purchased` ,DATE_ADD( MAX( `Posting Date` ), INTERVAL DATEDIFF( MAX( `Posting Date` ), MIN( `Posting Date` ) ) / COUNT( * ) DAY ) as `You will run out on` FROM `credit` GROUP BY `Description`;
示例结果(mysql):
Description Order every # Days Average Price Last Purchased You will run out on
Item 24 67.2857 29.8571428571429 06/08/16 12:00 AM 08/14/16 12:00 AM
Item 4 113 90 04/06/16 12:00 AM 07/28/16 12:00 AM
Item 57 37.0909 152.545454545455 06/21/16 12:00 AM 07/28/16 12:00 AM
Item 9 55.5556 54.8888888888889 05/27/16 12:00 AM 07/22/16 12:00 AM
Item 28 18.9286 127.035714285714 07/03/16 12:00 AM 07/22/16 12:00 AM
Item 42 16.5882 11.7058823529412 07/05/16 12:00 AM 07/22/16 12:00 AM
Item 35 10.5385 21.0192307692308 07/06/16 12:00 AM 07/17/16 12:00 AM
Item 12 12.6098 65.2439024390244 07/03/16 12:00 AM 07/16/16 12:00 AM
Item 25 22.2917 6.66666666666667 06/22/16 12:00 AM 07/14/16 12:00 AM
Item 38 9.4038 29.3846153846154 07/03/16 12:00 AM 07/12/16 12:00 AM
Item 3 17.5667 35.0333333333333 06/22/16 12:00 AM 07/10/16 12:00 AM
添加了订单,因此订购得更好,并为一周的购物减少了 7 天的清单。
SELECT `Category`, DATEDIFF( MAX( `Posting Date` ), MIN( `Posting Date` ) ) / COUNT( * ) AS `Order every # Days`, SUM( `Amount` ) / COUNT( * ) AS `Average Price`, MAX( `Posting Date` ) as `Last Purchased` ,DATE_ADD( MAX( `Posting Date` ), INTERVAL DATEDIFF( MAX( `Posting Date` ), MIN( `Posting Date` ) ) / COUNT( * ) DAY ) as `You will run out on`
FROM `credit`
GROUP BY `Category`
HAVING `You will run out on` < (NOW() + INTERVAL 7 DAY)
ORDER by `You will run out on` DESC;
如果您在 microsoft access 或 libre office base 中创建数据库,则上述 select 语句应该完成大部分繁重的工作。我在 mysql 数据库中进行了测试以进行测试,但除了细微的调整之外,它应该可以工作。
- 注意时间戳既是列的名称,也是数据类型
- 时间戳是访问中的 DATE/TIME
- 您需要一个项目列作为字符串来存储您购买的内容。
- 列名为 price 的价格。
- 如果您购买不同的数量,则必须更改公式。说 64 盎司的东西 1 次和 128 盎司。
- 我买了 5 盒的数量,不是 48 盎司或 10 磅。
如何创建表:mysql
MariaDB [test]> describe credit;
+----------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+--------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Originating Account Number | varchar(100) | YES | | NULL | |
| Posting Date | datetime | YES | | NULL | |
| Trans Date | datetime | YES | | NULL | |
| Type | varchar(100) | YES | | NULL | |
| Category | varchar(100) | YES | | NULL | |
| Merchant Name | varchar(100) | YES | | NULL | |
| Merchant City | varchar(100) | YES | | NULL | |
| Merchant State | varchar(100) | YES | | NULL | |
| Description | varchar(100) | YES | | NULL | |
| Transaction Type | varchar(100) | YES | | NULL | |
| Amount | double | YES | | NULL | |
| Reference Number | varchar(100) | YES | | NULL | |
+----------------------------+--------------+------+-----+---------+----------------+
13 rows in set (0.00 sec)