我有下表:
Order Product Price Quantity Description
Order1 Product1 12 1 Text
Order1 Product2 15 2 Text
Order1 Product3 32 1 Text
Order2 Product1 25 2 Text
Order2 Product4 65 3 Text
我需要显示订单1(产品,价格,数量,描述)的详细信息,以及该订单的总价值。有没有简单的方法呢?
答案 0 :(得分:3)
由于Order
是SQL关键字,因此您需要根据数据库引用它。
,例如,对于SQL Server:
select m.*, ms.TotalValue
from MyTable m
inner join (
select [Order], sum(Price * Quantity) as TotalValue
from MyTable
group by [Order]
) ms on m.[Order] = ms.[Order]
答案 1 :(得分:0)
假设总价值=总和(价格*数量)则:
select t.[Order],
t.Product,
t.Price,
t.Quantity,
t.Description,
aux.Total
from tablename t
inner join (
select [Order],
SUM(Price * Quantity) as 'Total'
from tablename
group by [Order]
) aux on t.[Order] = aux.[Order]
如果你想要每行的总数,你可以这样做:
select t.[Order],
t.Product,
t.Price,
t.Quantity,
t.Description,
t.Price * t.Quantity as 'Total'
from tablename t