将平均列添加到同一表中 [英] add average column into same table

查看:47
本文介绍了将平均列添加到同一表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的表:表1 我需要在该表中添加一列作为平均投放时间添加了avr_del_time列我的代码是:

I have a table like: Table1 And I need to add a column to that table as average delivery time avr_del_time column added My code is:

SELECT a.vendor, a.part_nr, a.delivery_time, b.avr_del_time
FROM  Table1 a
INNER JOIN (SELECT AVG(delivery_time) AS avr_del_time FROM Table1 GROUP BY vendor, part_nr) b
ON a.vendor = b.vendor, a.part_nr=b.part_nr

请指导我...

推荐答案

您的查询在正确的轨道上.您只需要解决SQL错误:

Your query is on the right track. You just need to fix up the SQL mistakes:

SELECT a.vendor, a.part_nr, a.delivery_time, b.avr_del_time
FROM Table1 as a INNER JOIN
      (SELECT  vendor, part_nr, AVG(delivery_time) AS avr_del_time
       FROM Table1
       GROUP BY vendor, part_nr
     ) as b
     ON a.vendor = b.vendor AND a.part_nr = b.part_nr;

注意:

  • GROUP BY 子查询中,您需要 SELECT 中的键.
  • 您在 ON 条件下有一个逗号;它应该是 AND .
  • 我建议您使用表别名(表名的缩写).
  • In the GROUP BY subquery, you need the keys in the SELECT.
  • You have a comma in the ON condition; it should be AND.
  • I would suggest that you use table aliases that are abbreviations for the table names.

这篇关于将平均列添加到同一表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆