gpt4 book ai didi

mysql - 每次对 DISTINCT 值进行 SQL SUM

转载 作者:行者123 更新时间:2023-11-29 12:39:33 25 4
gpt4 key购买 nike

我有下表。第一列是对象 ID,然后是对象大小,最后是特定对象的销售时间。

Object  Price           Moment
A 100 2014-10-10 22:12:00
B 105 2014-10-10 22:12:44
A 100 2014-10-10 22:14:12
C 203 2014-10-10 22:15:02
B 105 2014-10-10 22:17:34
D 68 2014-10-10 22:42:01
C 203 2014-10-10 22:43:02

通过使用以下 SQL,我得到每 15 分钟售出的所有元素的价格总和。

SELECT
Date(Moment) Date,
sec_to_time(time_to_sec(Moment) - time_to_sec(Moment)%(15*60)) 15MinInterval,
Sum(Price) PriceSum
FROM table
GROUP BY
Date,
15MinInterval
ORDER BY
Date,
15MinInterval

这效果很好,我得到的结果是:

2014-10-10 22:00:00 305
2014-10-10 22:15:00 308
2014-10-10 22:30:00 271

我需要的是每 15 分钟计算一次所有 DISTINCT 对象的价格总和,但我找不到方法。因此,对于特定的表,我需要得到以下结果:

2014-10-10 22:00:00 205
2014-10-10 22:15:00 308
2014-10-10 22:30:00 271

你能帮忙吗?

最佳答案

尝试在子查询中使用DISTINCT

SQL Fiddle:http://sqlfiddle.com/#!2/fc040/9

SELECT
Date,
15Interval 15MinInterval,
Sum(Price) PriceSum
FROM
(SELECT DISTINCT
OBJECT,
Price,
Date(Moment) Date,
sec_to_time(time_to_sec(Moment) - time_to_sec(Moment)%(15*60)) AS 15Interval
FROM mytable) temp
GROUP BY
Date,
15MinInterval
ORDER BY
Date,
15MinInterval

输出

enter image description here

关于mysql - 每次对 DISTINCT 值进行 SQL SUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26309899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com