简体   繁体   中英

SQL Server calculated column using group by

I'm attempting to create a calculated column using 3 columns from a separate table grouped by a foreign key in SSMS. What I have is a linking table called OrderLines that links the Orders and Products tables. The OrderLines table contains a RowID(PK), ItemPrice, Quantity, and OrderID(FK). The Orders table contains the primary key of OrderID and is the table I'd like to add the SubTotal column to. So the formula I need is ((ItemPricexQuantity) grouped by OrderID) but I can't figure out a solution.

OrderLines Table

OrdersTable

I've figured out how to get it as a column using a query and can create it using a view but was wondering if it can be added to the Orders table as a calculated column.

The query is that creates the column I need is:

Select orderid, SUM(itemprice*Quantity)
from orderitems
group by orderID

Thank you for your time, Stephen

Discussion earlier: How do I Specify Computed Columns in a Table which are based on another Column in SQL Server?

 create table orderitems ( orderId int, itemid int, productId int, itemprice decimal(18,2), quantity int ) GO 
 create table orders ( orderId int, shipAmount decimal(18,2), TaxAmount decimal(18,2) ) GO 
 insert into orderitems (orderId,itemid,productId,itemprice,quantity) values (1,1,2,1199,1), (2,2,8,489.99,1), (3,3,1,2517,1), (3,4,9,415,1), (4,5,2,1199,1), (5,6,10,299,1), (6,7,10,299,1) GO 
 insert into orders (orderId,shipAmount,TaxAmount) values (1,5,58.71), (2,5,58.72), (3,10,58.73), (4,10,58.74), (5,5,58.75), (6,5,58.76); GO 
 select * from orders GO 
\norderId |  shipAmount |  TaxAmount \n------: |  :--------- |  :-------- \n      1 |  5.00 |  58.71     \n      2 |  5.00 |  58.72     \n      3 |  10.00 |  58.73     \n      4 |  10.00 |  58.74     \n      5 |  5.00 |  58.75     \n      6 |  5.00 |  58.76     \n
 create function udfMyTable (@orderID as int) returns decimal(18, 2) as begin declare @res as decimal(19, 2); Select @res = SUM(itemprice*Quantity) from orderitems where orderID = @orderID return @res; end GO 
 alter table orderitems add SubTotal as dbo.udfMyTable(orderID) GO 
 alter table orders add SubTotal as dbo.udfMyTable(orderID) GO 
 Select * from orderitems GO 
\norderId |  itemid |  productId |  itemprice |  quantity |  SubTotal \n------: |  -----: |  --------: |  :-------- |  -------: |  :------- \n      1 |  1 |  2 |  1199.00 |  1 |  1199.00  \n      2 |  2 |  8 |  489.99 |  1 |  489.99   \n      3 |  3 |  1 |  2517.00 |  1 |  2932.00  \n      3 |  4 |  9 |  415.00 |  1 |  2932.00  \n      4 |  5 |  2 |  1199.00 |  1 |  1199.00  \n      5 |  6 |  10 |  299.00 |  1 |  299.00   \n      6 |  7 |  10 |  299.00 |  1 |  299.00   \n
 select * from orders GO 
\norderId |  shipAmount |  TaxAmount |  SubTotal \n------: |  :--------- |  :-------- |  :------- \n      1 |  5.00 |  58.71 |  1199.00  \n      2 |  5.00 |  58.72 |  489.99   \n      3 |  10.00 |  58.73 |  2932.00  \n      4 |  10.00 |  58.74 |  1199.00  \n      5 |  5.00 |  58.75 |  299.00   \n      6 |  5.00 |  58.76 |  299.00   \n

db<>fiddle here

i guess you need something below

select  O.orderID ,sum(itemprice*Quantity)Orders O inner join OrderLines ol 
on O.OrderID=ol.OrderId
group by  O.OrderID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM