仅将最新时间戳中的值包含到查询结果中

Only include values from the latest timestamp into the query result

DB-Fiddle

CREATE TABLE sales (
    id int auto_increment primary key,
    time_stamp TIMESTAMP,
    product VARCHAR(255),
    sales_quantity INT
);

INSERT INTO sales
(time_stamp, product, sales_quantity)
VALUES 
("2020-01-14 07:15:30", "Product_A", "100"),
("2020-01-14 07:15:30", "Product_B", "300"),
("2020-01-14 07:18:45", "Product_A", "200"),
("2020-01-14 07:18:45", "Product_B", "900"),

("2020-01-15 07:19:23", "Product_A", "400"),
("2020-01-15 07:19:23", "Product_B", "270"),
("2020-01-15 07:45:10", "Product_A", "900"),
("2020-01-15 07:45:10", "Product_B", "340");

预期结果:

time_stamp             sales_quantity
2020-01-14              1.100
2020-01-15              1.240

正如您在 table 中看到的那样,每天有多个 TIMESTAMP
现在,我想为每个 TIMESTAMP 查询 sales_quantitysum,但只有 lates ones 应该 included.

因此,在示例中 TIMESTAMP07:15:3007:19:23 应该被忽略。

我尝试使用此查询:

SELECT
MAX(time_stamp),
SUM(sales_quantity) AS sales_quantity
FROM sales
GROUP BY 1;

但是,我收到错误 Can't group on 'MAX(time_stamp)'
我需要如何修改查询以获得预期的结果?

尝试将产品包含在 select 列表中并对其进行分组,例如;

 SELECT
 product,
 MAX(time_stamp),
 SUM(sales_quantity) AS sales_quantity
 FROM sales
 GROUP BY product;

“按 1 分组”尝试根据第一列 max(time_stamp)

对其进行分组

一个选项使用相关子查询进行过滤:

select date(time_stamp) date_stamp, sum(product) sales_quantity
from sales s
where s.time_stamp = (
    select max(s1.time_stamp) 
    from sales s1 
    where s1.time_stamp >= date(s.time_stamp) and s1.time_stamp < date(s.time_stamp) + interval 1 day
)
group by date_stamp
order by date_stamp

如果你是运行MySQL8.0,也可以用rank():

select date(time_stamp) date_stamp, sum(product) sales_quantity
from (
    select s.*, rank() over(partition by date(time_stamp) order by time_stamp desc) rn
    from sales s
) t
where rn = 1
group by date_stamp
order by date_stamp

您可以使用 CTE 对每天的 time_stamp 值进行降序排列(我们使用 DENSE_RANK 以便 所有 行最新的 time_stamp 获得行号 1),然后 SUM 排名为 1 的那些行的数量(即当天的最新值):

WITH CTE AS (
  SELECT time_stamp,
         sales_quantity,
         DENSE_RANK() OVER (PARTITION BY DATE(time_stamp) ORDER BY time_stamp DESC) AS rn
  FROM sales
)
SELECT DATE(time_stamp) AS time_stamp,
       SUM(sales_quantity) AS sales_quantity
FROM CTE
WHERE rn = 1
GROUP BY time_stamp

输出:

time_stamp  sales_quantity
2020-01-14  1100
2020-01-15  1240

Demo on dbfiddle

您需要在 WHERE 子句中包含 NOT EXISTS 的条件,以便您仅聚合每天最新 time_stamp 的行:

SELECT DATE(s.time_stamp) time_stamp,
       SUM(s.sales_quantity) sales_quantity
FROM sales s
WHERE NOT EXISTS (SELECT 1 FROM sales WHERE DATE(time_stamp) = DATE(s.time_stamp) AND time_stamp > s.time_stamp)
GROUP BY DATE(s.time_stamp);

参见demo

如果您使用的是 MySql 8.0+ 而不是 MariaDB 10.3(比如您的 fiddle),那么您也可以使用 FIRST_VALUE() window 函数:

SELECT DISTINCT
       DATE(s.time_stamp) time_stamp,
       FIRST_VALUE(SUM(s.sales_quantity)) OVER (PARTITION BY DATE(s.time_stamp) ORDER BY s.time_stamp DESC) sales_quantity
FROM sales s
GROUP BY s.time_stamp;

参见demo

结果:

> time_stamp | sales_quantity
> :--------- | -------------:
> 2020-01-14 |           1100
> 2020-01-15 |           1240