查询以查找平均加权价格

时间:2011-01-19 20:43:32

标签: sql oracle

我在Oracle中有一个表,每个给定部分有多行。每行都有与之相关的数量和价格。还有一个总数量,给定部分的行集合加起来。以下是数据样本。我需要的是获得零件的平均加权价格。例如,如果零件的100的数量为1,而数量50的价格为2,则加权平均价格为1.33333333

PART   TOTAL_QTY  QTY   PRICE_PER
----------------------------------
part1  317        244   27
part1  317        40    53.85
part1  317        33    24.15

想法?

2 个答案:

答案 0 :(得分:20)

试试这个:

SELECT part, SUM(qty*price_per)/SUM(qty)
  FROM <YOUR_TABLE>
GROUP BY part

答案 1 :(得分:0)

创建用户定义的聚合函数来计算加权平均值:

CREATE OR REPLACE TYPE WEIGHTED_AVG_O AS OBJECT ( 
  sum_of_weights NUMBER, 
  sum_of_weights_times_value NUMBER, 

  STATIC FUNCTION ODCIAGGREGATEINITIALIZE(cs_ctx IN OUT WEIGHTED_AVG_O) RETURN NUMBER, 
  MEMBER FUNCTION ODCIAGGREGATEITERATE   (self IN OUT WEIGHTED_AVG_O, value IN WEIGHTED_AVG_O) RETURN NUMBER, 
  MEMBER FUNCTION ODCIAGGREGATEMERGE     (self IN OUT WEIGHTED_AVG_O, ctx2 IN OUT WEIGHTED_AVG_O) RETURN NUMBER, 
  MEMBER FUNCTION ODCIAGGREGATETERMINATE (self IN OUT WEIGHTED_AVG_O, returnvalue OUT NUMBER, flags IN NUMBER) RETURN NUMBER 
);
/



CREATE OR REPLACE TYPE BODY WEIGHTED_AVG_O
AS

  STATIC FUNCTION ODCIAGGREGATEINITIALIZE(cs_ctx IN OUT WEIGHTED_AVG_O) RETURN NUMBER
  IS
  BEGIN
    cs_ctx := WEIGHTED_AVG_O(0, 0);
    RETURN odciconst.success;
  END;

  MEMBER FUNCTION ODCIAGGREGATEITERATE   (self IN OUT WEIGHTED_AVG_O, value IN WEIGHTED_AVG_O) RETURN NUMBER
  IS
  BEGIN
    self.sum_of_weights := self.sum_of_weights + value.sum_of_weights;
    self.sum_of_weights_times_value := self.sum_of_weights_times_value + value.sum_of_weights * value.sum_of_weights_times_value;
    RETURN odciconst.success;
  END;

  MEMBER FUNCTION ODCIAGGREGATEMERGE     (self IN OUT WEIGHTED_AVG_O, ctx2 IN OUT WEIGHTED_AVG_O) RETURN NUMBER
  IS
  BEGIN
    RETURN odciconst.success;
  END;

  MEMBER FUNCTION ODCIAGGREGATETERMINATE (self IN OUT WEIGHTED_AVG_O, returnvalue OUT NUMBER, flags IN NUMBER) RETURN NUMBER 
  IS
  BEGIN    
    IF sum_of_weights = 0 THEN
      returnvalue := NULL;
    ELSE
      returnvalue := sum_of_weights_times_value / sum_of_weights;
    END IF;
    RETURN odciconst.success;
  END;

END;
/


CREATE OR REPLACE FUNCTION WEIGHTED_AVG (input WEIGHTED_AVG_O)
   RETURN NUMBER PARALLEL_ENABLE
   AGGREGATE USING WEIGHTED_AVG_O;
/

查询您的数据:

SELECT part, WEIGHTED_AVG(WEIGHTED_AVG_O(qty, price_per))
  FROM <YOUR_TABLE>
 GROUP BY part;