简体   繁体   English

SQL单组分组函数错误/非分组表达式

[英]SQL single-group group function error/Not a group-by expression

I'm trying to create a view, but when i include the "group by" function, it says that there is an error at line 2 saying that it is not a group-by expression. 我正在尝试创建一个视图,但是当我包含“ group by”功能时,它表示第2行有错误,表明它不是group-by表达式。 If i comment out the "group-by" function, then it says theres an single-group group function error in the view. 如果我注释掉“ group-by”功能,则表示视图中存在单组分组功能错误。 how can i fix this? 我怎样才能解决这个问题?

CREATE or REPLACE VIEW bestseller_view
  AS SELECT e.empl_name,
            NVL((sum((si.amt_paid+a.ao_price)-fs.purchase_cost+a.ao_purchase_price)*se.salary_comm),0) sales_commission,
            COUNT(si.s_empl_id) vehicles_sold
 FROM for_sale fs
 JOIN sales_invoice si ON fs.vehicle_id = si.vehicle_id
 JOIN sales_empl se    ON si.s_empl_id = se.empl_id
 JOIN purchased_ao pao ON si.sales_invoice_id=pao.sales_invoice_id
 JOIN add_ons a        ON pao.ao_id=a.ao_id
 JOIN employee e       ON se.empl_id=e.empl_id;
 GROUP BY si.s_empl_id;

The non-aggregate column in your select is e.empl_name , so you need to group by that, not si.s_empl_id; 在您的非聚合列selecte.empl_name ,所以你需要组通过,不si.s_empl_id; :

... SELECT e.empl_name,
  NVL((sum(...),0) sales_commission,
  COUNT(...) vehicles_sold
...
GROUP BY e.empl_name;

But since empl_name presumably isn't guaranteed to be unique, grouping by the ID makes more sense. 但是由于不能保证empl_name唯一,因此按ID进行分组更有意义。 You need to either select and group by both columns - though using e.empl_id might look more logical - and just ignore the ID you get back if it isn't relevant, in this case by just never selecting it from the view; 您需要选择和按两列进行e.empl_id -尽管使用e.empl_id可能看起来更合逻辑-并且只要它不相关就忽略您返回的ID,在这种情况下,只需从视图中不选择它即可; or use this as an inner query and don't include it in the outer one. 或将其用作内部查询,请勿将其包含在外部查询中。 Seems like it makes more sense to have it in the view in case anyone does want/need it one day. 好像将它放在视图中似乎更有意义,以防万一有人确实想要/需要它。

... SELECT e.empl_id, e.empl_name,
  NVL((sum(...),0) sales_commission,
  COUNT(...) vehicles_sold
...
GROUP BY e.empl_id, e.empl_name;

You also have your parentheses in the wrong place in your sum : 你也可以在你放错了地方的括号sum

NVL((sum((si.amt_paid+a.ao_price)-fs.purchase_cost+a.ao_purchase_price)
     *se.salary_comm),0) sales_commission,

... is calculating the sum of all the (si.amt_paid+a.ao_price)-fs.purchase_cost+a.ao_purchase_price values, and then multiplying that sum by the se.salary_comm , which makes that column non-aggregate as well; ...正在计算所有(si.amt_paid+a.ao_price)-fs.purchase_cost+a.ao_purchase_price值的总和, 然后将该总和乘以se.salary_comm ,这也使该列se.salary_comm聚合。 I'm pretty sure that's not what you meant to do, so that should be: 我很确定那不是您的本意,所以应该是:

NVL(sum((si.amt_paid+a.ao_price-fs.purchase_cost+a.ao_purchase_price)
  *se.salary_comm),0) sales_commission,

Or if there is a one-to-one relationship between e and se , which looks likely, you could leave that as it is and add se.salary_comm to the group by clause as @ypercube suggested. 或者,如果ese之间存在一对一的关系(看起来很可能),则可以将其保留se.salary_comm ,并按照@ypercube的建议将se.salary_comm添加到group by子句中。

 SELECT e.empl_name, si.s_empl_id
 ...
 GROUP BY e.empl_name, si.s_empl_id 

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

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