Получение синтаксической ошибки при использовании group by в операторе обновления в SQL Server - PullRequest
0 голосов
/ 20 февраля 2019

Получение синтаксической ошибки при использовании group by в операторе обновления SQL Server

    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

Ответы [ 3 ]

0 голосов
/ 20 февраля 2019

вы можете использовать подвыбор

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 голосов
/ 20 февраля 2019

Вы не можете использовать агрегаты напрямую с 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 голосов
/ 20 февраля 2019

Попробуйте ниже - кажется, что вам не нужна группа по

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') 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...