Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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
Sql 用于添加带有外键的相同列条目的查询_Sql_Oracle11g - Fatal编程技术网

Sql 用于添加带有外键的相同列条目的查询

Sql 用于添加带有外键的相同列条目的查询,sql,oracle11g,Sql,Oracle11g,我有下表和属性: vendor_invoicedetails Venid(Pk) ven_inv_ref(Fk) Item_Code Item_Name UnitPrice VenQuantity 我想用VenQuantity乘以unitPrice,得到我在以下查询中得到的总价 select item_code, VEN_INV_REF, unitprice * ven_itemquantity as total from

我有下表和属性:

vendor_invoicedetails
   Venid(Pk)
   ven_inv_ref(Fk)
   Item_Code
   Item_Name
   UnitPrice
   VenQuantity
  • 我想用
    VenQuantity
    乘以
    unitPrice
    ,得到我在以下查询中得到的总价

    select item_code, 
           VEN_INV_REF, 
           unitprice * ven_itemquantity as total
      from vendor_invoicedetails;
    
  • 我想要的是将
    TotalPrice
    相加为两个相同的
    veninv\u Ref(Fk)
    列的总和

  • 在上面的图片中,我希望对具有相同编号的条目求和。

    尝试以下方法:

    DECLARE @datatable TABLE
       (
         ITEM_CODE NVARCHAR(32) PRIMARY KEY CLUSTERED,
         ven_inv_ref NVARCHAR(50),
         Item_Name NVARCHAR(50),
         UnitPrice FLOAT, 
         VenQuantity INT
       )
    
    INSERT INTO @datatable
    (ITEM_CODE, ven_inv_ref, UnitPrice, VenQuantity)
    VALUES ('battery', 15, 100, 4)
    
    INSERT INTO @datatable
    (ITEM_CODE, ven_inv_ref, UnitPrice, VenQuantity)
    VALUES ('ABCDE', 16, 200, 4)
    
    INSERT INTO @datatable
    (ITEM_CODE, ven_inv_ref, UnitPrice, VenQuantity)
    VALUES ('A4', 16, 400, 4)
    
    -- whats in the table   
    SELECT * 
    FROM @datatable
    
    -- group by reference
    SELECT 
        ven_inv_ref, 
        SUM(UnitPrice*VenQuantity) AS totalvalue 
    FROM @datatable
    GROUP BY ven_inv_ref
    
    结果