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

  • SEARCH
  • Home
  • 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 8975555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:54:43+00:00 2026-06-15T18:54:43+00:00

Is there a better way to write this query: INSERT INTO #Temp (MinDateTime, VehicleID)

  • 0

Is there a better way to write this query:

    INSERT INTO #Temp (MinDateTime, VehicleID)
    SELECT Min(CAST(RDate AS smalldatetime) + RTime) as MinDateTime, T.VehicleID
    FROM (
        SELECT D.RDate, D.RTime, D.VehicleID
        FROM DELETED AS D 
        JOIN INSERTED AS I ON D.ReceiptID = I.ReceiptID
        AND (D.[RDate] <> I.[RDate] OR D.[RTime] <> I.[RTime] OR D.VehicleID <> I.VehicleID OR D.Liters <> I.Liters OR D.OdometerReading <> I.OdometerReading)
        UNION ALL
        SELECT I.RDate, I.Rtime, I.VehicleID
        FROM INSERTED AS I 
        JOIN DELETED AS D ON I.ReceiptID = D.ReceiptID
        AND (I.[RDate] <> D.[RDate] OR I.[RTime] <> D.[RTime] OR I.VehicleID <> D.VehicleID OR I.Liters <> D.Liters OR I.OdometerReading <> D.OdometerReading)
        UNION ALL
        SELECT D.RDate, D.RTime, D.VehicleID
        FROM DELETED AS D 
        LEFT JOIN INSERTED AS I ON D.ReceiptID = I.ReceiptID
        WHERE I.ReceiptID IS NULL
        UNION ALL
        SELECT I.RDate, I.Rtime, I.VehicleID
        FROM INSERTED AS I 
        LEFT JOIN DELETED AS D ON I.ReceiptID = D.ReceiptID
        WHERE D.ReceiptID IS NULL
    ) AS T
    GROUP BY T.VehicleID
    ORDER BY MinDateTime, T.VehicleID

This is part of an after update trigger that evaluates records who have been deleted/added/inserted and looks at one of the 5 columns if they’ve been modified it generates a list of vehicles and date/time so that the trigger can execute a stored proc that recalculates affected records.

  • 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-06-15T18:54:44+00:00Added an answer on June 15, 2026 at 6:54 pm

    I suspect the inner query could be written as:

        SELECT D.RDate, D.RTime, D.VehicleID
        FROM DELETED D full outer JOIN
             INSERTED I
             ON D.ReceiptID = I.ReceiptID
        where ((D.[RDate] <> I.[RDate] OR D.[RTime] <> I.[RTime] OR D.VehicleID <> I.VehicleID OR D.Liters <> I.Liters OR D.OdometerReading <> I.OdometerReading) or
               (I.[RDate] <> D.[RDate] OR I.[RTime] <> D.[RTime] OR I.VehicleID <> D.VehicleID OR I.Liters <> D.Liters OR I.OdometerReading <> D.OdometerReading) or
             I.ReceiptID IS NULL or
             D.ReceiptID IS NULL
            ) and
            (coalesce(i.ReceiptId, d.receiptId) is not null)  -- check that at least one has a value
    

    In other words, I replaced the sequence of unions with a full outer join and then put all the conditions together in a where clause, separated by ors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way to write this SQL query? SELECT *, (SELECT TOP
Is there a better simplified way to write this query. My logic is if
I have this query and I know there is a better way to write
Is there a better way to write this spec? This works but I don't
I am wondering if there is a better way to write this JavaScript (Jquery)
is there any way to write this foreach in linq or another better way,
is there a better way to write this SQL than using WHERE ... IN
There must be a better way to write this, but I'm just not sure
Is there a better / more efficient / shorter way to write this SQL
Is there a better way to write the following function? Having the '#' +

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.