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 8609157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:48:38+00:00 2026-06-12T03:48:38+00:00

I create a temporary table #tbl(account, last_update). I have following two inserts from different

  • 0

I create a temporary table #tbl(account, last_update). I have following two inserts from different source (could be tables from different databases) to insert account with last update date. For example

create table #tbl ([account] numeric(18, 0), [last_update] datetime)

insert into #tbl(account , last_update)
select table1.account, max(table1.last_update)
from table1 join…
group by table1.account

insert into #tbl(account , last_update)
select table2.account, max(table2.last_update)
from table2 join…
group by table2.account

The problem is this could cause duplicate account in the table #tbl. I either have to avoid it during each insert or remove the duplicate after both insert. Also, if there is account with two different last_update, I want the #tbl have the latest last_update. How do I achieve this conditional insert? Which one will have better performance?

  • 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-12T03:48:39+00:00Added an answer on June 12, 2026 at 3:48 am

    Do you think you could rewrite your query to something like:

    create table #tbl ([account] numeric(18, 0), [last_update] datetime)
    
    insert into #tbl(account , last_update)
      select theaccount, MAX(theupdate) from 
      (
        select table1.account AS theaccount, table1.last_update AS theupdate
        from table1 join…
         UNION ALL
        select table2.account AS theaccount, table2.last_update AS theupdate
        from table2 join…
       ) AS tmp GROUP BY theaccount 
    

    The UNION ALL will build you 1 unique table combining table1 + table2 records. From there, you can act as if was a regular table, which means that you are able to find the max last_update for each record using a “group by”

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

Sidebar

Related Questions

I have the following tables: create temporary table Items (item_id int, item_name varchar(10)); create
I have a string like following: CREATE GLOBAL TEMPORARY TABLE some_temp_table_name or CREATE GLOBAL
I have a sample file like the following: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 (
I was kinda surprised when I saw following: CREATE TEMPORARY TABLE X (ID int)
Can we create a temporary table from the stored procedure results dynamically? I do
As we create and drop temporary tables, inserts data into those tables, the size
If i have this table on dataBase.js file: client.query( 'CREATE TEMPORARY TABLE IF NOT
I have a table called leaderboard which I create create temporary table leaderboard (
I have a table with timestamped rows: say, some feed with authors: CREATE TEMPORARY
I am running the following queries against a database: CREATE TEMPORARY TABLE med_error_third_party_tmp SELECT

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.