我正在尝试从一个表中获取最新行,该表记录了每15分钟的累积销售总额。
表的基本布局是
StoreID, Time, Sales, Tax, Date
我希望能够返回每个商店ID的最新记录。到目前为止,我尝试过的查询是:
SELECT t.StoreID, MAX(t.Time) as 'Last Reported', t.Sales+t.Tax as Sales, t.Date FROM ( SELECT * FROM trickledata WHERE Date = '20180724' ) t GROUP BY t.StoreID;
这有效,但是暂时不返回相应的销售额。例如。
+---------+---------------+-------+------------+
| StoreID | Last Reported | Sales | Date |
+---------+---------------+-------+------------+
| 100 | 11:45:00 | 0.00 | 2018-07-24 |
| 111 | 12:00:00 | 0.00 | 2018-07-24 |
| 115 | 12:00:00 | 0.00 | 2018-07-24 |
| 121 | 12:00:00 | 0.00 | 2018-07-24 |
| 122 | 12:00:00 | 0.00 | 2018-07-24 |
| 123 | 12:00:00 | 0.00 | 2018-07-24 |
| 124 | 12:00:00 | 0.00 | 2018-07-24 |
| 125 | 12:00:00 | 0.00 | 2018-07-24 |
| 126 | 12:00:00 | 0.00 | 2018-07-24 |
| 127 | 12:00:00 | 0.00 | 2018-07-24 |
| 128 | 12:00:00 | 0.00 | 2018-07-24 |
| 129 | 12:00:00 | 0.00 | 2018-07-24 |
| 130 | 12:00:00 | 0.00 | 2018-07-24 |
| 131 | 12:00:00 | 0.00 | 2018-07-24 |
| 135 | 12:00:00 | 0.00 | 2018-07-24 |
| 137 | 12:00:00 | 0.00 | 2018-07-24 |
| 138 | 12:00:00 | 0.00 | 2018-07-24 |
| 141 | 12:00:00 | 0.00 | 2018-07-24 |
| 150 | 12:00:00 | 0.00 | 2018-07-24 |
| 160 | 12:00:00 | 0.00 | 2018-07-24 |
| 164 | 12:00:00 | 0.00 | 2018-07-24 |
但是当时有销售,因为当我查询商店121的特定日期和时间时,我得到:
+---------+------------+----------+-------+------+
| StoreID | Date | Time | Sales | Tax |
+---------+------------+----------+-------+------+
| 121 | 2018-07-24 | 12:00:00 | 65.79 | 4.55 |
+---------+------------+----------+-------+------+
CREATE TABLE语句:
``CREATE TABLE `trickledata` (
`StoreID` INT(11) NULL DEFAULT NULL,
`Date` DATE NULL DEFAULT NULL,
`Time` TIME NULL DEFAULT NULL,
`Sales` DECIMAL(10,2) NULL DEFAULT NULL,
`Tax` DECIMAL(10,2) NULL DEFAULT NULL,
UNIQUE INDEX `Date` (`Date`, `Time`, `StoreID`),
INDEX `by_date` (`Date`),
INDEX `by_store` (`StoreID`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;
``
样本数据:
INSERT LOW_PRIORITY IGNORE INTO `trickle`.`trickledata_copy` (`StoreID`, `Date`, `Time`, `Sales`, `Tax`) VALUES ('0100', '20180724', '120000', '212.63', '15.37'), ('0100', '20180724', '114500', '212.63', '15.37'), ('0100', '20180724', '113000', '212.63', '15.37'), ('0100', '20180724', '111500', '212.63', '15.37'), ('0100', '20180724', '110000', '212.63', '15.37'), ('0100', '20180724', '104500', '212.63', '15.37'), ('0100', '20180724', '103000', '212.63', '15.37'), ('0100', '20180724', '101500', '.00', '.00'), ('0100', '20180724', '100000', '.00', '.00'), ('0100', '20180724', '94500', '.00', '.00'), ('0100', '20180724', '93000', '.00', '.00'), ('0100', '20180724', '91500', '.00', '.00'), ('0100', '20180724', '90000', '.00', '.00'), ('0100', '20180724', '84500', '.00', '.00'), ('0100', '20180724', '83000', '.00', '.00'), ('0100', '20180724', '81500', '.00', '.00'), ('0100', '20180724', '80000', '.00', '.00'), ('0100', '20180724', '74500', '.00', '.00'), ('0100', '20180723', '253000', '986.07', '71.44'), ('0100', '20180723', '251500', '986.07', '71.44'), ('0100', '20180723', '250000', '986.07', '71.44'), ('0100', '20180723', '244500', '986.07', '71.44'), ('0100', '20180723', '243000', '986.07', '71.44'), ('0100', '20180723', '241500', '986.07', '71.44'), ('0100', '20180723', '240000', '986.07', '71.44'), ('0100', '20180723', '234500', '986.07', '71.44'), ('0100', '20180723', '233000', '986.07', '71.44'), ('0100', '20180723', '231500', '986.07', '71.44'), ('0100', '20180723', '230000', '986.07', '71.44'), ('0100', '20180723', '224500', '986.07', '71.44'), ('0100', '20180723', '223000', '986.07', '71.44'), ('0100', '20180723', '221500', '986.07', '71.44'), ('0100', '20180723', '220000', '986.07', '71.44'), ('0100', '20180723', '214500', '986.07', '71.44'), ('0100', '20180723', '213000', '986.07', '71.44'), ('0100', '20180723', '120000', '986.07', '71.44'), ('0100', '20180723', '210000', '986.07', '71.44'), ('0100', '20180723', '204500', '986.07', '71.44');
编辑#2:
使用该样本数据并运行查询会产生0.00错误。但是跑步
SELECT * FROM trickledata WHERE Time = '120000
的结果为$ 212.63
我的MariaDB版本也是10.1.29
编辑#3: 我尝试了以下方法:
''SELECT t.* FROM trickledata t
-> JOIN (
-> SELECT StoreID, MAX(Time) AS latest, Sales, Date
-> FROM trickledata
-> WHERE Date = '20180724'
-> GROUP BY StoreID
-> ) m ON t.StoreID = m.StoreID AND t.Time = m.latest;''
但是,它返回我中午的每个日期。我只想要今天的数据。
编辑#4: 我发现如果删除唯一索引,我的初始查询将按预期工作。但是我不明白为什么。另外,我需要该唯一索引来防止添加重复项,因为这是从csv文件中读取信息。
答案 0 :(得分:0)
尝试使用相关子查询过滤时间,该查询将获得一天的最大时间并存储。
SELECT `t1`.`storeid` `StoreId`,
`t1`.`time` `Last Reported`,
`t1`.`sales` + `t1`.`tax` `Sales`,
`t1`.`date` `Date`
FROM `trickledata` `t1`
WHERE `t1`.`date` = '2018-07-24'
AND `t1`.`time` = (SELECT max(`t2`.`time`)
FROM `trickledata` `t2`
WHERE `t2`.`storeid` = `t1`.`storeid`
AND `t2`.`date` = `t1`.`date`);
如果有重复项(同一天同一时间同一商店的两个或更多个报告),将显示它们。如果要排除它们,则必须定义另一个条件,该条件应该在报告上定义其他顺序。