gpt4 book ai didi

mysql - 具有条件的 SUM 子查询取决于父查询列返回 NULL

转载 作者:搜寻专家 更新时间:2023-10-30 23:26:11 24 4
gpt4 key购买 nike

大家好!

我正在尝试计算每天的交易价格总和。我做什么:

SET @symbols_set = "A,B,C,D";

DROP TABLE IF EXISTS temp_deals;

CREATE TABLE temp_deals AS SELECT Deal, TimeMsc, Price, VolumeExt, Symbol FROM deals WHERE TimeMsc >= "2019-04-01" AND TimeMsc <= "2019-06-30" AND FIND_IN_SET(Symbol, @symbols_set) > 0;

SELECT
DATE_FORMAT(TimeMsc, "%d/%m/%Y") AS Date,
Symbol,
(SELECT SUM(Price) FROM temp_deals dap WHERE dap.TimeMsc BETWEEN Date AND Date + INTERVAL 1 DAY AND dap.Symbol = Symbol) AS AvgPrice
FROM temp_deals
ORDER BY Date;

DROP TABLE IF EXISTS temp_deals;

但结果我在 AvgPrice 列中得到了 NULL。我不明白我做错了什么。

看起来我不能将父查询的列传递给子查询,对吗?

最佳答案

限定您的列名称。但大多数情况下,不要使用字符串来比较日期:

SELECT DATE_FORMAT(d.TimeMsc, '%d/%m/%Y') AS Date,
d.Symbol,
(SELECT SUM(dap.Price)
FROM temp_deals dap
WHERE dap.TimeMsc >= d.TimeMsc AND
dap.TimeMsc < d.TimeMsc + INTERVAL 2 DAY AND -- not sure if you want 1 day or 2 day
dap.Symbol = d.Symbol
) AS AvgPrice
FROM temp_deals d
ORDER BY d.TimeMsc;

关于mysql - 具有条件的 SUM 子查询取决于父查询列返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57659362/

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