Sql 添加绑定到特定主键的不同行中的值

Sql 添加绑定到特定主键的不同行中的值,sql,sql-server,Sql,Sql Server,我试图添加绑定到特定主键的期权金额,然后将其作为列添加到末尾。这是我的密码。我的零售成本达到了最大值 SELECT AppDetailVehicleValuation.AppID, VehicleValuationOption.Description AS AddOn, MAX(VehicleValuationOptionValueType.Value) AS RetailCost FROM AppDetailVehicleValuat

我试图添加绑定到特定主键的期权金额,然后将其作为列添加到末尾。这是我的密码。我的零售成本达到了最大值

SELECT
    AppDetailVehicleValuation.AppID, 
    VehicleValuationOption.Description AS AddOn, 
    MAX(VehicleValuationOptionValueType.Value) AS RetailCost
FROM            
    AppDetailVehicleValuation
    INNER JOIN VehicleValuationOption 
        ON AppDetailVehicleValuation.ValuationID = VehicleValuationOption.ValuationID 
    INNER JOIN VehicleValuationOptionValueType 
        ON VehicleValuationOption.ValuationOptionID = VehicleValuationOptionValueType.ValuationOptionID
WHERE        
    (VehicleValuationOption.IsSelected LIKE '1') 
    AND (VehicleValuationOption.IsSystemOption LIKE '1')
GROUP BY VehicleValuationOption.Description, AppDetailVehicleValuation.AppID
这是我的

AppID | AddOn       | RetailCost
999     Beats Audio   475.00
999     Safety Tek    675.00
1052    Tinted Win    850.00
1052    18in Rims     1600.00
1052    Matte Pt.     950.00
这就是我想要实现的目标

AppID | AddOn       | RetailCost   | AddOnTotal
999     Beats Audio   475.00         1150.00
999     Safety Tek    675.00         1150.00
1052    Tinted Win    850.00         3400.00
1052    18in Rims     1600.00        3400.00
1052    Matte Pt.     950.00         3400.00   
使用窗口功能:

with t as (<your query here>)
select appid, addon, retailcost,
       sum(retailcost) over (partition by appid) as addontotal
from t;
带t作为()
选择appid、加载项、零售成本、,
总计(零售成本)超过(按appid划分)作为addontotal
从t;