简体   繁体   English

AVG()正在复制存储在行中的数据,但未提供该列的平均值

[英]AVG() is replicating data stored in the row and not providing the average number for the column

I've created a query to retrieve data from multiple columns. 我创建了一个查询以从多个列中检索数据。 However, when using the AVG(*) AVERAGE to display the value for a given column, the function simply replicates and displays the data in each row. 但是,当使用AVG(*)AVERAGE显示给定列的值时,该函数仅复制并显示每一行中的数据。 I essentially want the table to display each column in the query and display the table average in each row. 我本质上是希望表显示查询中的每一列,并在每一行中显示表的平均值。

If I don't have multiple columns in the select statement then I can query the table to display the true average for the given column just fine. 如果select语句中没有多个列,则可以查询表以显示给定列的真实平均值。 I have tried using using UNIONs and SELF JOINs to query the AVG(*) AVERAGE, but with no luck. 我尝试使用UNION和SELF JOIN来查询AVG(*)AVERAGE,但是没有运气。

CREATE TABLE INVOICE
(
    INV_NUM NUMBER NOT NULL PRIMARY KEY,
    CUST_NUM NUMBER NOT NULL REFERENCES CUSTOMER(CUST_NUM),
    INV_DATE DATE NOT NULL,
    INV_AMOUNT NUMBER NOT NULL
);


INSERT ALL
INTO (INV_NUM,CUST_NUM,INV_DATE,INV_AMOUNT) 
VALUES ('8000','1000','3/23/2014','235.89')


INTO (INV_NUM,CUST_NUM,INV_DATE,INV_AMOUNT) 
VALUES ('8001','1001','3/23/2014','312.82')


INTO (INV_NUM,CUST_NUM,INV_DATE,INV_AMOUNT) 
VALUES ('8002','1002','3/30/2014','528.10')       


INTO (INV_NUM,CUST_NUM,INV_DATE,INV_AMOUNT) 
VALUES ('8003','1003','4/12/2014','194.78')


INTO (INV_NUM,CUST_NUM,INV_DATE,INV_AMOUNT) 
VALUES ('8004','1004','4/23/2014','619.44')

SELECT * FROM DUAL;

--------------TABLE QUERIES----------------

SELECT INV_NUM, INV_AMOUNT, AVG(INV_AMOUNT) AVERAGE, 
AVG(INV_AMOUNT)-INV_AMOUNT DIFFERENCE
FROM INVOICE
GROUP BY INV_NUM, INV_AMOUNT;

Something like this: 像这样:

with invoice as (
select 
8000 INV_NUM,1000 CUST_NUM,'3/23/2014' INV_DATE,235.89 INV_AMOUNT
from dual union all
select 8001,1001,'3/23/2014',312.82
from dual union all
select 8002,1002,'3/30/2014',528.10
from dual union all
select 8003,1003,'4/12/2014',194.78
from dual union all
select 8004,1004,'4/23/2014',619.44 from dual)
SELECT INV_NUM, INV_AMOUNT, AVERAGE, 
AVERAGE-INV_AMOUNT DIFFERENCE
FROM INVOICE,(select avg(inv_amount) average from invoice)
ORDER BY INV_NUM, INV_AMOUNT

You are aggregating by INV_NUM and INV_AMT , so you have not done any aggregation at all. 您正在按INV_NUMINV_AMT进行聚合,因此您根本没有进行任何聚合。

I think you want an analytic function instead of aggregation: 我认为您想要一个分析功能而不是聚合:

SELECT INV_NUM, INV_AMOUNT,
       AVG(INV_AMOUNT) OVER () as AVERAGE, 
       (AVG(INV_AMOUNT) OVER () - INV_AMOUNT) as DIFFERENCE
FROM INVOICE;

暂无
暂无

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

相关问题 从列中获取平均值,然后过滤掉数量小于平均值的所有行,然后计算行数 - Get the average from a column, then filter out all the rows whose number is smaller than the average, and then count the number of row 在SQL Server中使用AVG在计算平均值时排除行 - Exclude row while calculation average using AVG in sql server 移动平均:连续计算具有最近N个数据的AVG - Moving average: Compute AVG with Last N data continuosly 计算平均值 (AVG),包括 Redshift DB 中某个日期范围内的缺失数据 - Calculate average (AVG) including missing data in a date range in Redshift DB 动态枢轴中的行和列平均值 - Row and column average in dynamic pivot 当所有数据都存储为字符串时,有没有办法计算事件发生的平均次数? - Is there a way to calculate the average number of times an event happens when all data is stored as string? 如何根据存储在 Postgresql 列中的 json 数据获取行 - how to fetch row on the basis of json data stored in a column in Postgresql 从子查询和 SQL SELECT 中的 ROW_NUMBER 窗口函数生成“平均”列 - Generate 'average' column from sub query and ROW_NUMBER window function in SQL SELECT 如何将偶数行号数据添加到第一列中,如何将奇数行号数据添加到第二列中? - how to add even row number data into first column and odd row number data into the second column? 如果列为avg,Sql,如何按desc或asc排序行 - How do I order the row by the desc or asc if the column is avg,Sql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM