在SQL Server的更新语句中使用group by时获取语法错误

时间:2019-02-20 09:50:53

标签: sql sql-server sql-update

在SQL Server的更新语句中使用group by时出现语法错误

<div>Bar</div>

3 个答案:

答案 0 :(得分:0)

尝试以下方法-看来您不需要任何分组依据

Update m1 set   
              Event_Code =  max(case when m.Status_Description = 'DPKL' then l.Status_Description else NULL end),                                                  
              Create_Date = max(case when m.Status_Description = 'DPKL' then dateadd(dd,datediff(dd,0,l.Move_Create_Timestamp),0) else NULL end), 
              PTimeStamp=max(case when m.Status_Description = 'DPKL' then l.Move_Create_Timestamp else NULL end), 
              Acrual_Date=max(case when m.Status_Description = 'DPKL' then dateadd(dd,datediff(dd,0,l.Move_Status_Timestamp),0) else NULL end) 
FROM LegMove m1 inner join wrkLegMove m on m.leg_key=m1.leg_key
inner join MovementMaster l with(nolock) on l.Leg_Key = m.Leg_Key and l.Status_Description = m.Status_Description and l.Move_Create_Timestamp = m. FirstMoveTime 
where m.Status_Description in ('DPKL') 

答案 1 :(得分:0)

您可以使用子选择

update lm set Event_Code = mx.Event_Code, 
    Create_Date = mx.Create_Date, 
    PTimeStamp = mx.PTimeStamp, 
    Acrual_Date = mx.Acrual_Date
from LegMove lm
    inner join (
    select m.leg_key,
        Event_Code =  max(case when m.Status_Description = 'DPKL' then l.Status_Description else NULL end),                                                  
        Create_Date = max(case when m.Status_Description = 'DPKL' then dateadd(dd,datediff(dd,0,l.Move_Create_Timestamp),0) else NULL end), 
        PTimeStamp=max(case when m.Status_Description = 'DPKL' then l.Move_Create_Timestamp else NULL end), 
        Acrual_Date=max(case when m.Status_Description = 'DPKL' then dateadd(dd,datediff(dd,0,l.Move_Status_Timestamp),0) else NULL end) 
    FROM wrkLegMove m 
        inner join MovementMaster l with(nolock) on l.Leg_Key = m.Leg_Key and l.Status_Description = m.Status_Description and l.Move_Create_Timestamp = m. FirstMoveTime 
    --if you youse the filter then the case is useless?
    where m.Status_Description in ('DPKL') 
    group by m.Leg_Key
    ) mx on m.Leg_Key = lm.leg_key

答案 2 :(得分:0)

您不能将聚合直接与UPDATE一起使用,因为您要将行粉碎在一起以进行聚合,并且引擎不清楚链接原始行的方法。

在子查询或CTE中计算您的聚合,然后对您的表进行联接以通过您的键进行更新:

;WITH AggregatedData AS
(
    SELECT
        -- I'm assuming these columns are your key on LegMove table
        m.Leg_Key, 
        l.Shipment_Number, 
        l.Shipment_Leg_Sequence,

        -- Aggregated values to udpate
        Event_Code = max(case when m.Status_Description = 'DPKL' then l.Status_Description else NULL end),                                                  
        Create_Date = max(case when m.Status_Description = 'DPKL' then dateadd(dd,datediff(dd,0,l.Move_Create_Timestamp),0) else NULL end), 
        PTimeStamp = max(case when m.Status_Description = 'DPKL' then l.Move_Create_Timestamp else NULL end), 
        Acrual_Date = max(case when m.Status_Description = 'DPKL' then dateadd(dd,datediff(dd,0,l.Move_Status_Timestamp),0) else NULL end) 
    FROM 
        wrkLegMove m 
        inner join MovementMaster l with(nolock) on 
            l.Leg_Key = m.Leg_Key and 
            l.Status_Description = m.Status_Description and 
            l.Move_Create_Timestamp = m. FirstMoveTime 
    where 
        m.Status_Description in ('DPKL') 
    group by 
        m.Leg_Key, 
        l.Shipment_Number, 
        l.Shipment_Leg_Sequence

)
UPDATE L SET
    Event_Code = A.Event_Code,
    Create_Date = A.Create_Date, 
    PTimeStamp = A.PTimeStamp,
    Acrual_Date = A.Acrual_Date
FROM
    LegMove AS L
    INNER JOIN AggregatedData AS A ON
        L.Leg_Key = A.Leg_Key AND
        L.Shipment_Number = A.Shipment_Number AND
        L.Shipment_Leg_Sequence = A.Shipment_Leg_Sequence

请注意,如果您加入的列少于要聚合的列,则更新的值将不一致,因为您将与要更新的值和SQL建立1到N的关系服务器将自由决定更新哪个。