简体   繁体   中英

Compute 2 subqueries then group by date - mysql

i have this Table

附图片

i want to do subqueries first then add them together grouped by date

Expected Result should be like this:

随附的

am running this query

(
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
)

and getting this result

随附的

Appreciated your help

Use JOIN between subqueries and every next one:

(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

If I followed you correctly, you can use union all and aggregation:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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