包含必填字段组合的合计

b4lqfgs4  于 2021-08-13  发布在  Java
关注(0)|答案(3)|浏览(302)

我有一个带有svl2\u名称的表,其中包含grad、country、region和其他svl2\u名称中的avg count\u emp,并更新tot\u avg\u grad列

create table GRADE_S1
(
  SVL2_NAME varchar2(29),
  GRADE    NUMBER,
  COUNTRY  VARCHAR2(20),
  REGION   varchar2(20),
  COUNT_EMP NUMBER,
  TOT_AVG_GRAD FLOAT
);

insert into GRADE_S1 values('TY',10,'CANADA','AMERICAS',3,null);
insert into GRADE_S1 values('TY',10,'CHINA','APJC',4,null);
insert into GRADE_S1 values('TY',9,'CHINA','APJC',6,null);
insert into GRADE_S1 values('TY',12,'FRANCE','EMEA',8,null);
insert into GRADE_S1 values('Anuj',10,'CANADA','AMERICAS',4,null);
insert into GRADE_S1 values('Anuj',10,'CHINA','APJC',6,null);
insert into GRADE_S1 values('Anuj',12,'FRANCE','EMEA',2,null);
insert into GRADE_S1 values('kumar',10,'CANADA','AMERICAS',4,null);
insert into GRADE_S1 values('kumar',10,'CHINA','APJC',4,null);
insert into GRADE_S1 values('kumar',9,'CHINA','APJC',6,null);
insert into GRADE_S1 values('kumar',12,'FRANCE','EMEA',6,null);

需要一个汇总表如下。

Name Grad Country Region   Count_emp Tot_Avg_Emp
---- ---- ------- -------- --------- -----------
TY     10 CANADA  AMERICAS         3           4
TY     10 CHINA   APJC             4           5
TY      9 CHINA   APJC             6           3
TY     12 FRANCE  EMEA             8           4

我在考虑创建一个类似
第一步:

--FOR Loop
select * from GRADE_S1 where SVL2_NAME='TY';

第二步:

select AVG(COUNT_EMP)  INTO V_AVG_EMP
  from GRADE_S1
 where GRAD=INX.GRAD
   AND COUNTRY=INX.COUNTRY
   AND REGION = INX.REGION
   AND SVL2_NAME <> INX.SVL2_NAME

第三步:

update GRADE_S1 set
  TOT_AVG_GRAD =V_AVG_EMP
  WHERE SVL2_NAME = INX.SVL2_NAME
    AND GRAD=INX.GRAD
    AND COUNTRY=INX.COUNTRY
    AND REGION = INX.REGION

或者有什么办法。。。?
谢谢

yxyvkwin

yxyvkwin1#

使用单个 MERGE 语句关联 ROWID 伪列并使用分析函数计算所有匹配行的平均值,要从平均值中除去当前行,只需找到总数并减去当前值和计数,然后减去1:

MERGE INTO Grade_s1 dst
USING(
  SELECT ROWID AS rid,
         CASE COUNT(*) OVER ( PARTITION BY grade, country, region )
         WHEN 1
         THEN NULL -- Avoid division by zero
         ELSE ( SUM(count_emp) OVER ( PARTITION BY grade, country, region ) - count_emp )
              / ( COUNT(*) OVER ( PARTITION BY grade, country, region ) - 1 )
         END AS tot_avg_grad
  FROM   grade_s1
) src
ON ( dst.ROWID = src.RID )
WHEN MATCHED THEN
  UPDATE
  SET tot_avg_grad = src.tot_avg_grad;

然后:

SELECT *
FROM   grade_s1;

输出:

SVL2_NAME | GRADE | COUNTRY | REGION   | COUNT_EMP | TOT_AVG_GRAD
:-------- | ----: | :------ | :------- | --------: | -----------:
TY        |    10 | CANADA  | AMERICAS |         3 |            4
TY        |    10 | CHINA   | APJC     |         4 |            5
TY        |     9 | CHINA   | APJC     |         6 |            6
TY        |    12 | FRANCE  | EMEA     |         8 |            4
Anuj      |    10 | CANADA  | AMERICAS |         4 |          3.5
Anuj      |    10 | CHINA   | APJC     |         6 |            4
Anuj      |    12 | FRANCE  | EMEA     |         2 |            7
kumar     |    10 | CANADA  | AMERICAS |         4 |          3.5
kumar     |    10 | CHINA   | APJC     |         4 |            5
kumar     |     9 | CHINA   | APJC     |         6 |            6
kumar     |    12 | FRANCE  | EMEA     |         6 |            5

db<>在这里摆弄

u4vypkhs

u4vypkhs2#

选择
svl2\U名称,
等级,
国家,
区域,
伯爵阁下,
(
选择
平均值(计数)

等级s1 g2
哪里
g2.grade=g1.grade
和g2.country=g1.country
g2.region=g1.region
和g2.svl2_名称<>g1.svl2_名称
)总平均梯度

等级s1 g1

zf2sa74q

zf2sa74q3#

这似乎只是一个窗口函数:

select SVL2_NAME, GRADE, COUNTRY, REGION, count_emp,
       avg(count_emp) over (partition by country, region) as Tot_Avg_Emp
from grade_s1;

如果要筛选特定名称,请使用子查询:

select g.*
from (select SVL2_NAME, GRADE, COUNTRY, REGION, count_emp,
             avg(count_emp) over (partition by country, region) as Tot_Avg_Emp
      from grade_s1
     ) g
where svl2_name = 'Ty';

我不鼓励实际将数据写入表中。但如果你真的想,你可以 update 使用相关子查询:

update grade_s1 g
    set Tot_Avg_Emp = (select avg(count_emp)
                       from grade_s1 g2
                       where g2.country = t.country and t2.region = t.region
                      )
    where svl2_name = 'Ty';

相关问题