【问题标题】:how to add value of other column in comboamount column如何在comboamount列中添加其他列的值
【发布时间】:2018-05-18 10:29:06
【问题描述】:

我有以下查询任何人 SQL Server 大师请帮帮我,我很紧张, 我有两列 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

【问题讨论】:

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


    【解决方案1】:

    我认为您只需要一个累积总和 - 这需要一个排序列。据我所知,这是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
    

    【讨论】:

    • 他希望实时更新 ComboAmount 与该特定表中 TotalPrice 的比例。如果我没记错的话。
    • @Sameer 。 . .我在问题中没有看到任何暗示 update 而不是在查询中返回所需结果的内容。
    【解决方案2】:

    您可以将计算列用于此类工作,也可以根据需要编写存储/触发器。检查以下链接以获取您的查询:

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

    【讨论】:

      【解决方案3】:

      所以请下次在图像中放入文本而不是文本,就像你的 cummaltive 句子一样,我不知道它正在累积!

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-17
        • 2015-10-01
        • 2020-08-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多