简体   繁体   English

如何在不设置sql模式的情况下在mysql中与group by聚合?

[英]How do I aggregate with group by in mysql withou setting sql mode?

This query gets the output I want. 该查询获取我想要的输出。 In order to run it I have to run 为了运行它,我必须运行

SET sql_mode = '';

Because otherwise I get an error: 因为否则我会得到一个错误:

SELECT list is not in GROUP BY clause and contains nonaggregated column 'knownLoss.t1.loss' which is not functionally dependent on columns in GROUP BY clause; SELECT列表不在GROUP BY子句中,并且包含未聚合的列'knownLoss.t1.loss',该列在功能上不依赖于GROUP BY子句中的列; this is incompatible with sql_mode=only_full_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

When I place this into the Dataset and Query Dialog of Jasper Studio, I get the same error and I am unable to use the SET sql_mode = ''; 当我将其放入Jasper Studio的“数据集和查询”对话框时,出现相同的错误,并且无法使用SET sql_mode =''; command. 命令。 Any thoughts? 有什么想法吗? If there is a way to achieve this without using SET sql_mode = '';? 如果有一种方法可以不使用SET sql_mode ='';?

I'm guessing the error is from this line in your select: 我猜测错误是来自您选择的这一行:

round(t1.loss / t2.loss,2) AS Percentage

Since you GROUP BY clause does not include this column it's somewhat of a coin toss which t1.loss and t2.loss values will be used. 由于您的GROUP BY子句不包含此列,因此有点像抛硬币,将使用t1.losst2.loss值。 In some cases those values happen to always be the same based on your criteria and so you get the correct results regardless, but the db will still complain since it's being asked to return somewhat arbitrary results for those columns. 在某些情况下,根据您的标准,这些值总是相同的,因此无论如何您都会获得正确的结果,但是由于仍然要求数据库返回那些列的任意结果,因此数据库仍然会抱怨。 One way to deal with this would be to simply apply an aggregate function to the columns in question like this: 一种解决方法是简单地将聚合函数应用于所关注的列,如下所示:

round(min(t1.loss) / min(t2.loss),2) AS Percentage

or... 要么...

round(avg(t1.loss) / avg(t2.loss),2) AS Percentage

I think you want to do this : 我认为您想这样做:

round(sum(t1.loss / t2.loss)/count(*),2)  AS Percentage

this will calculate the sum of the average loss for every records in the result then divide it on the the count of the record of the group it's like average of average. 这将计算结果中每条记录的平均损失总和,然后将其除以该组的记录计数,这就像平均值的平均值。

EDITS: 编辑:

sorry i made a syntax error now ,it should give the th wanted result and the error is because you are not using aggregate function on a column that is not in group by clause 抱歉,我现在犯了一个语法错误,它应该给出th想要的结果,并且该错误是因为您不在不在group by子句的列上使用聚合函数

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

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