如何在comboamount列中添加其他列的值

时间:2018-05-18 10:29:06

标签: sql sql-server sql-server-2008 sql-server-2012

我有以下查询任何SQL服务器主人帮我这个请我很紧张, 我有两列ComboAmmount和TotalPrice,我想要TotalPrice在ComboAmount中有一些价格添加。添加Allmount ComboAmount显示最终价格后,请查看下面的图片

select CAST( CASE WHEN a.TotalPrice != 0 THEN 1
 ELSE 0  END AS bit) as CheckOtherPrice,   a.OrderDetailId,a.ItemId,b.ItemCode,b.ItemName,e.GroupName,a.Quantity,a.Price,a.TotalPrice,a.OrderId,isnull(a.ComboId,0)ComboId,
 a.ComboGroupId,a.ComboAmount,a.ComboUniqueId,c.ComboName,d.GroupName ComboGroupName,a.OfferMasterId from OrderDetail a
 inner join ItemMaster b on a.ItemId=b.ItemId and a.OrderId in (983)
inner join ItemGroup e on b.ItemGroupId=e.ItemGroupId
left join ComboMaster c on a.ComboId = c.ComboId
left join ComboGroup d on a.ComboGroupId = d.GroupId

Please Check this image

3 个答案:

答案 0 :(得分:0)

我认为你只需要一笔累积金额 - 这需要一个订购栏。据我所知,这是orderdetailid

以下显示了此的逻辑。它还用表缩写替换表别名。这使得查询更容易理解:

select . . .,
       ( od.ComboAmount + sum(od.TotalPrice) over (order by od.orderdetailid) ) as new_comboamount
from OrderDetail od inner join
     ItemMaster im
     on od.ItemId = im.ItemId and
        od.OrderId in (983) inner join
     ItemGroup ig
     on im.ItemGroupId = ig.ItemGroupId left join
     ComboMaster cm
     on od.ComboId = cm.ComboId left join
     ComboGroup cg
     on od.ComboGroupId = cg.GroupId

答案 1 :(得分:0)

您可以根据需要将计算列用于此类工作或写入存储/触发器。请查看以下链接以查询您的信息:

How to multiply two columns and assign its output to the column of another table?

答案 2 :(得分:0)

所以请下次把文字放在图片中,而不是像你的暨句子一样的文字,我不知道它在累积!!

How to get cumulative sum这是你的答案......使用sum并拆分表..