Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 用第一个表的平均值填充第二个表_Mysql_Sql_Transactions_Average - Fatal编程技术网

Mysql 用第一个表的平均值填充第二个表

Mysql 用第一个表的平均值填充第二个表,mysql,sql,transactions,average,Mysql,Sql,Transactions,Average,我有两张桌子,交易和清单 事务列出我每天发生的所有事务。我在交易中有一列,列出了每个交易项目的价格。每天发生几笔交易。我想取每天所有交易的中位数或平均值,并用这些信息填充列表中的新列 因此,我的最终结果是在清单中有一个名为daily_price_average的列,它从交易中获取单个交易信息的平均价格 你有什么想法吗 或者,如何使用视图执行此操作?使用 平均而言 INSERT INTO averages (day, average) SELECT date, AVG(price) GRO

我有两张桌子,交易和清单

事务列出我每天发生的所有事务。我在交易中有一列,列出了每个交易项目的价格。每天发生几笔交易。我想取每天所有交易的中位数或平均值,并用这些信息填充列表中的新列

因此,我的最终结果是在清单中有一个名为daily_price_average的列,它从交易中获取单个交易信息的平均价格

你有什么想法吗

或者,如何使用视图执行此操作?

使用

平均而言

INSERT INTO averages (day, average)
  SELECT date, AVG(price)
  GROUP BY date

获得中位数是

您可以在以下视图中执行此操作:

create v_listings as
    select l.*,
           (select avg(price) 
            from transactions t
            where date(t.transactiondate) = l.date
           ) as daily_price_average
    from listings l;
要进行更新,您首先要确保
daily\u price\u average
是列表中的一列:

update listings join
       (select date(t.transactiondate) as tdate, avg(price) as avgprice
        from transactions
        group by date(t.transactiondate)
       ) td
       on listings.date = td.tdate
    set daily_price_average = td.avgprice;

这两种方法都假设
列表
中有一列名为
日期
的平均值。

步骤1-确定您想要的是平均值还是中值。如果你想要中间值,决定你想要哪种类型。我会选择平均值。不要在表格中这样做,使用
视图