3

我有下表,带有属性:

vendor_invoicedetails
   Venid(Pk)
   ven_inv_ref(Fk)
   Item_Code
   Item_Name
   UnitPrice
   VenQuantity
  1. 我想将 with 相乘unitPriceVenQuantity获得我对以下查询所做的总价

    select item_code, 
           VEN_INV_REF, 
           unitprice * ven_itemquantity as total
      from vendor_invoicedetails;
    
  2. 我想要的是将TotalPrice两个相同Ven_inv_Ref(Fk)列的总计相加。

在此处输入图像描述

在上图中,我想对具有相同VEN_INV_REF编号的条目求和。

4

1 回答 1

4

试试这个:

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

结果

在此处输入图像描述

于 2012-11-30T07:57:05.170 回答