-3

i have below query anyone SQL server master help me this please i am very tensed, i have two column ComboAmmount and TotalPrice, i want if TotalPrice have some price add in ComboAmount . After Add Allmount ComboAmount Show Final Price please check below picture

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

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
john
  • 13
  • 4

3 Answers3

0

I think you just need a cumulative sum -- and that requires an ordering column. From what I can tell, this is orderdetailid.

The following shows the logic for this. It also replaces the table aliases with table abbreviations. This makes the query much easier to follow:

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
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • He wish to update ComboAmount in real time with proportion to TotalPrice in that particular table. If I am not wrong. – Sameer May 18 '18 at 10:42
  • @Sameer . . . I don't see anything in the question that suggests an `update` as opposed to returning the desired result in a query. – Gordon Linoff May 18 '18 at 12:37
0

You can use computed column for such nature of work or Write Stored/Trigger as your wish. Check below link for your query:

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

Sameer
  • 349
  • 4
  • 12
0

so Please next time put text isntead of an text in an image like your cummaltive sentence, i didnt know it was accumulating!!

How to get cumulative sum this is your answer... use sum and split the table..

Tito
  • 601
  • 8
  • 23