Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:32:19+00:00 2026-05-25T22:32:19+00:00

CREATE TABLE IntegrationLog ( IntegrationLogID INT IDENTITY(1,1) NOT NULL, RecordID INT NOT NULL, SyncDate

  • 0
CREATE TABLE IntegrationLog (
IntegrationLogID INT IDENTITY(1,1) NOT NULL,
RecordID INT NOT NULL,
SyncDate DATETIME NOT NULL, 
Success BIT NOT NULL,
ErrorMessage VARCHAR(MAX) NULL,
PreviousError BIT NOT NULL --last sync attempt for record failed for syncdate
)

My goal here, is to return every recordid, erorrmessage that has not been followed by a complete success, exclude where for the recorid there was a ( Success == 1 and PreviousError == 0) that occurred after the last time this error happened. For this recordid, I also want to know whether there has ever been a success ( Partial or otherwise ) that has ever happened.

Or in other words, I want to see errors and the record they occurred on that haven’t been fixed since the error occurred. I also want to know whether I have ever had a success for the particular recordid.

This works, but I am curious if there is a better way to do this?

SELECT  errors.RecordID ,
        errors.errorMessage,
        CASE WHEN PartialSuccess.RecordID IS NOT NULL THEN 1
             ELSE NULL
        END AS Resolved
FROM    ( SELECT    errors.RecordID ,
                    errors.ErrorMessage ,
                    MAX(SyncDate) AS SyncDate
          FROM      dbo.IntegrationLog AS Errors
          WHERE     errors.Success = 0
          GROUP BY errors.RecordID ,
                    errors.ErrorMessage ,
                    errors.ErrorDescription
        ) AS Errors
        LEFT JOIN dbo.IntegrationLog AS FullSuccess ON FullSuccess.RecordID = Errors.RecordID
                                                              AND FullSuccess.Success = 1
                                                              AND FullSuccess.PreviousError = 0
                                                              AND FullSuccess.SyncDate > Errors.SyncDate
        LEFT JOIN ( SELECT  partialSuccess.RecordID
                    FROM    dbo.IntegrationLog AS partialSuccess
                    WHERE   partialSuccess.Success = 1
                    GROUP BY partialSuccess.RecordID
                  ) AS PartialSuccess ON Errors.RecordID = PartialSuccess.RecordID
WHERE   FullSuccess.RecordID IS NULL

I also created a pastebin with a few different ways I saw of structuring the query. http://pastebin.com/FtNv8Tqw
Is there another option as well?

If it helps, background for the project is that I am trying to sync records that have been updated since their last successful sync ( Partial or Full ) and log the attempts. A batch of records is identified to be synced. Each record attempt is logged. If it failed, depending on the error it might be possible try to massage the data and attempt again. For this ‘job’, the time we collected the records is used as the SyncDate. So for a given SyncDate, we might have records that successfully synced on the first try, records we gave up on the first attempt, records we massaged and were able to sync, etc. Each attempt is logged.

Does it change anything if instead of wanting to know whether any success has occurred for that recordid, that I wish to identify whether a partial success has occurred since the last error occurrence.

Thank You! Suggestions on my framing of the question are welcome as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:32:20+00:00Added an answer on May 25, 2026 at 10:32 pm

    You should probably show query plan take a look at where most of the time is being spent and index appropriately.

    That said one thing you can try is to use the Window Function ROW_NUMBER instead of MAX.

    WITH cte 
         AS (SELECT errors.recordid, 
                    errors.errormessage, 
                    CASE 
                      WHEN partialsuccess.recordid IS NOT NULL THEN 1 
                      ELSE NULL 
                    END 
                       AS resolved, 
                    Row_number() OVER (PARTITION BY errors.recordid ORDER BY 
                    syncdate 
                    DESC) 
                       rn 
             FROM   integrationlog error 
                    LEFT JOIN integrationlog fullsuccess 
                      ON fullsuccess.recordid = errors.recordid 
                         AND fullsuccess.success = 1 
                         AND fullsuccess.previouserror = 0 
                         AND fullsuccess.syncdate > errors.syncdate 
                    LEFT JOIN (SELECT partialsuccess.recordid 
                               FROM   dbo.integrationlog AS partialsuccess 
                               WHERE  partialsuccess.success = 1 
                               GROUP  BY partialsuccess.recordid) AS partialsuccess 
                      ON errors.recordid = partialsuccess.recordid 
             WHERE  errors.success = 0) 
    SELECT 
         recordid,
         errormessage,
          resolved
    FROM   cte 
    WHERE  rn = 1 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

create table ImagenesUsuario { idImagen int primary key not null IDENTITY } This doesn't
Create table FavoriteDish ( FavID int identity (1,1) primary key not null, DishID int
CREATE TABLE `users` ( `UID` INT UNSIGNED NOT NULL AUTO_INCREMENT , `username` VARCHAR(45) NOT
CREATE TABLE Customer( customer_id INT NOT NULL, first_name VARCHAR(20), last_name VARCHAR(20), PRIMARY KEY (customer_id)
create table A ( id int(10) not null, val1 varchar(255), primary key (id) );
CREATE TABLE IF NOT EXISTS `Channels` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30)
CREATE TABLE `users` ( `ID` int(10) unsigned zerofill NOT NULL auto_increment, `username` varchar(20) NOT
CREATE TABLE `sequence` (`id` int(11) NOT NULL auto_increment, `load_status` varchar(100) default NULL, PRIMARY KEY
CREATE TABLE [schema].[table] ( [column1] int IDENTITY NOT NULL, [column2] int NULL, [column3] int
CREATE TABLE `pastebin` ( `pid` int(11) NOT NULL auto_increment, `poster` varchar(16) default NULL, `posted`

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.