Avatar of Paul Mauriello
Paul Mauriello
Flag for United States of America asked on

SQL Server 2014 query- trying to mark records for deletion based on an aggregate total within the same table?

I'm trying to mark records for deletion based on an aggregate total within the same table?       If an aggregate grouping with the tblVesselMessageBunkerConsumption  is zero then I want to mark those rows for deletion  but not the ones that total greater then zero. Where am I going wrong because no matter what I do they end up all being marked?

            UPDATE c
               SET c.IsActive = 0,
                     c.IsDelete = 1
            FROM
             tblVesselMessage a
             JOIN tblVesselMessageBunkerConsumption c ON a.VesselMessageID = c.VesselMessageID
            CROSS APPLY (SELECT VesselMessageID,
                               UsageUserDictionaryID,
                               EngineUserDictionaryID,
                               isSlr,
                               RowDisplayOrder,
                               Quantity=SUM(ISNULL(Quantity,0)) ,
                               IsActive,
                               IsDelete
                         FROM tblVesselMessageBunkerConsumption
                          WHERE VesselMessageID = c.VesselMessageID
                           GROUP BY  VesselMessageID,
                                           UsageUserDictionaryID,
                                           EngineUserDictionaryID,
                                           isSlr,
                                           RowDisplayOrder,
                                           Quantity,
                                           IsActive,
                                           IsDelete )  b
       
            WHERE
                   a.EVFAcctID = 2
                   AND b.isActive = 1
                   AND b.isDelete = 0
                   AND b.RowDisplayOrder IS NOT NULL
                   AND b.Quantity = 0
                   AND c.isActive = 1
                   AND c.isDelete = 0
                   AND c.RowDisplayOrder IS NOT NULL
                   AND c.VesselMessageID = 15196
Microsoft SQL Server

Avatar of undefined
Last Comment
Paul Mauriello

8/22/2022 - Mon
ste5an

It's the missing correlation to your sub-query in the CROSS APPLY. You're calculating the same SUM for all rows with the same VesselMessageID.
Paul Mauriello

ASKER
How would I fix it?
ste5an

Correlate it.

Without a concise and complete example it's impossible to tell what the correlating columns could be.. a time stamp, a row number, a counter?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Paul Mauriello

ASKER
Thank you