繁体   English   中英

计算 2 个子查询,然后按日期分组 - mysql

[英]Compute 2 subqueries then group by date - mysql

我有这张桌子

附图片

我想先做子查询,然后将它们按日期分组

预期结果应该是这样的:

随附的

正在运行此查询

(
SELECT DATE_FORMAT(dd1.modified_datetime,'%Y-%m-%d') as date, (v1+v2) as value FROM
    (SELECT modified_datetime, Sum(data->"$.amount") as v1
        FROM transactions
        GROUP BY modified_datetime) as dd1 ,            
    (SELECT modified_datetime, MAX(data->"$.amount") as v2
        FROM transactions
        GROUP BY modified_datetime) as dd2

    GROUP BY dd1.modified_datetime, value
)

并得到这个结果

随附的

感谢您的帮助

在子查询和每个下一个查询之间使用JOIN

(SELECT modified_datetime, Sum(data->"$.amount") as v1
    FROM transactions
    GROUP BY modified_datetime) as dd1 JOIN
(SELECT modified_datetime, MAX(data->"$.amount") as v2
    FROM transactions
    GROUP BY modified_datetime) as dd2 ON dd1.modified_datetime=dd2.modified_datetime

如果我正确地跟随你,你可以使用union all和聚合:

select date_format(dt, '%Y-%m-%d') dt_day, sum(amount) value
from (
    select modified_datetime dt, data ->> '$.amount' amount from transactions
    union all 
    select created_datetime, data ->> '$.amount' from transactions
) t
group by dt_day
order by dt_day

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM