【问题标题】:Average from multiple columns per row in SQLSQL中每行多列的平均值
【发布时间】:2023-01-31 20:42:51
【问题描述】:

我想在 SQL 中执行此操作

但是价格 2 和价格 3 来自另一个表,也使用 sql 连接。

如何在sql中做到这一点?

【问题讨论】:

  • Please do not upload images of code/data/errors or even the task description. 很多人看不到你在问什么,因为我们看到的只是“我想在SQL中执行此操作 enter image description here But the price 2 and price 3 come from another table and joined using sql too.”
  • 很不清楚为什么你不能这样做:(Price1 + Price2 + Price3)/3
  • 添加相关信息作为文本,而不是图像。表模式、数据样本和预期结果。

标签: mysql sql multiple-columns average byrow


【解决方案1】:

这是您的问题的解决方案,请尝试并查看,如果您使用的是单个表,这是一个代码示例。尝试进行必要的更改以从另一个表中加入和拉取字段。

SELECT *,
 (SELECT AVG(tot) 
  FROM
  (VALUES(Price1),
         (Price2),
         (Price3)
  ) T (tot)) AS AverageOfColumns
FROM   MyTable;

或者这也应该有效,也很简单,但是如果您有大量的列,我更喜欢上面的列,

    select 
      Item
      ,sum(Price1+Price2+Price3)/3 
    from MyTable group by Item;

【讨论】:

    猜你喜欢
    • 2018-07-12
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    相关资源
    最近更新 更多