Mysql

如何通過依賴我的 sql 中的其他列來對列求和?

  • April 25, 2019

如何通過updateid列的最新數據計算列的/SUM?

我試試這個:

SELECT * FROM yourTable;

在此處輸入圖像描述

SELECT 
ANY_VALUE(code) AS code,
COUNT(*) AS total_rows,
SUM(data->'$.chart."21"') AS total_a
FROM yourTable
GROUP BY code
ORDER BY code;

從上面的查詢中,它對所有行求和

從上面的查詢中,它對所有行求和,它應該是最新的更新 id

任何人都可以解決這個問題?

這是小提琴:https ://www.db-fiddle.com/f/qHnaHNQtEhDasLiSB4Jhv5/0

SELECT 
t1.code,
COUNT(*) AS total_rows,
SUM(t1.data->'$.chart."21"') AS total_a
FROM yourTable AS t1
JOIN (SELECT code, MAX(updateid) AS updateid
     FROM yourTable
     GROUP BY code) AS t2 ON t1.code = t2.code
                         AND t1.updateid = t2.updateid
GROUP BY t1.code
ORDER BY t1.code;

引用自:https://dba.stackexchange.com/questions/236637