这个查询得到我想要的输出。为了运行它,我必须运行
SET sql_mode = '';
因为否则我会收到一个错误:
SELECT 列表不在 GROUP BY 子句中,并且包含非聚合列“knownLoss.t1.loss”,它在功能上不依赖于 GROUP BY 子句中的列;这与 sql_mode=only_full_group_by 不兼容
SELECT
t1.klDate AS LDate
, t1.Category
, t1.reason AS Reason
, sum(t1.loss) AS Loss
, round(t1.loss / t2.loss,2) AS Percentage
FROM
deptkl t1
JOIN (
SELECT
Store
, sum(loss) AS loss
FROM
deptkl
WHERE
klDate >= date_sub(SUBDATE(curdate(), WEEKDAY(curdate())), interval 7 day)
AND
klDate < SUBDATE(curdate(), WEEKDAY(curdate()))
AND
Store = 19
AND
Department = 80
) t2 ON t1.Store = t2.Store
WHERE
klDate >= date_sub(SUBDATE(curdate(), WEEKDAY(curdate())), interval 7 day)
AND
klDate < SUBDATE(curdate(), WEEKDAY(curdate()))
AND
t1.Store = 19
AND
Department = 80
GROUP BY
klDate
, Category
, reason
当我将它放入 Jasper Studio 的数据集和查询对话框时,我得到了同样的错误,我无法使用 SET sql_mode = ''; 命令。有什么想法吗?如果有一种方法可以在不使用 SET sql_mode = ''; 的情况下实现这一点?