如果我想对来自 2 个表的乘以 2 个值的项目求和,如何在 oracle 中创建过程

Posted

技术标签:

【中文标题】如果我想对来自 2 个表的乘以 2 个值的项目求和,如何在 oracle 中创建过程【英文标题】:how to create procedure in oracle if i want to sum items who are product of multiply 2 values from 2 tables 【发布时间】:2014-02-19 14:24:28 【问题描述】:

我想在 oracle 中创建程序,它将所有项目相加并告诉我所有项目是否 > 50.000。问题是我需要将来自 2 个不同表的值相乘。非常感谢任何帮助。这是下面的代码。

create or replace 
PROCEDURE         "SUMASTAVKI" (sifraRacuna IN NUMBER) AS  
suma NUMBER:=0;
BEGIN  
    SELECT SUM(KOLICINA * CENA) INTO suma  
    FROM "STAVKARACUNA" sr
    WHERE "RACUNID"=sifraRacuna; 
    if (suma>50000)
  then
  begin
  UPDATE "RACUN"
    SET "VREDNOST"=suma, "POPUST"='da'
    WHERE "RACUNID"=sifraRacuna;end;
  else
  begin
  UPDATE "RACUN"
    SET "VREDNOST"=suma, "POPUST"='ne'
    WHERE "RACUNID"=sifraRacuna;end;
  end if;
END;

它写一个错误:“CENA”是无效的标识符

表 PROIZVOD 的 DDL,其中 CENA 值为:

   CREATE TABLE "NST"."PROIZVOD" 
   (    "PROIZVODID" NUMBER(7,0), 
    "NAZIV" VARCHAR2(50 BYTE), 
    "CENA" NUMBER(7,2), 
     CONSTRAINT "PK_PROIZVOD" PRIMARY KEY ("PROIZVODID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "SYSTEM"  ENABLE
   ) SEGMENT CREATION IMMEDIATE 
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "SYSTEM" ;

表 STAVKARACUNA 的 DDL 是:

     CREATE TABLE "NST"."STAVKARACUNA" 
   (    "RACUNID" NUMBER(7,0), 
    "RB" NUMBER(7,0), 
    "PROIZVODID" NUMBER(7,0), 
    "KOLICINA" NUMBER(7,0), 
     CONSTRAINT "PK_STAVKARACUNA" PRIMARY KEY ("RACUNID", "RB")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "SYSTEM"  ENABLE, 
     CONSTRAINT "FK_RACUNNASTAVKURACUNA" FOREIGN KEY ("RACUNID")
      REFERENCES "NST"."RACUN" ("RACUNID") ON DELETE CASCADE ENABLE, 
     CONSTRAINT "FK_PROIZVODNASTAVKURACUNA" FOREIGN KEY ("PROIZVODID")
      REFERENCES "NST"."PROIZVOD" ("PROIZVODID") ON DELETE CASCADE ENABLE
   ) SEGMENT CREATION IMMEDIATE 
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "SYSTEM" ;

【问题讨论】:

那么你的问题到底是什么? 既然只要求Oracle,为什么还要将mysql放在类别中? 【参考方案1】:

如果我理解正确,您在程序中使用的选择应该如下:

SELECT SUM(sr.KOLICINA * pr.CENA) INTO suma  
FROM STAVKARACUNA" sr, "PROIZVOD" pr
WHERE "RACUNID" = sifraRacuna
AND sr.PROIZVODID = pr.PROIZVODID

所以你的程序是:

create or replace PROCEDURE "SUMASTAVKI" (sifraRacuna IN NUMBER) AS  
  suma NUMBER:=0;
BEGIN  
    SELECT SUM(sr.KOLICINA * pr.CENA) INTO suma  
    FROM STAVKARACUNA" sr, "PROIZVOD" pr
    WHERE "RACUNID" = sifraRacuna
    AND sr.PROIZVODID = pr.PROIZVODID; 
    if (suma>50000) then
      UPDATE "RACUN"
      SET "VREDNOST"=suma, "POPUST"='da'
      WHERE "RACUNID"=sifraRacuna;
    else
      UPDATE "RACUN"
      SET "VREDNOST"=suma, "POPUST"='ne'
      WHERE "RACUNID"=sifraRacuna;
  end if;
END;

【讨论】:

以上是关于如果我想对来自 2 个表的乘以 2 个值的项目求和,如何在 oracle 中创建过程的主要内容,如果未能解决你的问题,请参考以下文章

SQL Join 2个表,返回只存在1个值而没有其他值的记录

带连接的求和函数是用 mysql 将 laravel ORM 中条目的总和乘以 mysql

在 MSSM 中使用标量值函数和来自 2 个表的数据

在 s-s-rS Report Builder Execute(@Query) 中对列标题使用动态 SQL - 使用来自 1 个表的值作为来自不同表的值的列标题

Java - 合并来自不同数据源中的 2 个表的数据

一次查询中 2 个表的 MS Access 总和