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

问题描述 投票:0回答:3

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

    Update LegMove

        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 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
sql sql-server sql-update
3个回答
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

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

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


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') 
© www.soinside.com 2019 - 2024. All rights reserved.