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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:31:52+00:00 2026-06-18T02:31:52+00:00

I have two tables with the same column names, and the same composite primary

  • 0

I have two tables with the same column names, and the same composite primary key. The composite primary key of both tables is id + year. I want to always use data from table T1, and if there exists an id + year from table T2, that’s not in table T1 I want it to be returned also.

Here’s my tables

T1

id  |  year  |  cost
----+--------+-------
1   |  2012  |  1.01
2   |  2012  |  1.02
2   |  2013  |  1.03

T2

id  |  year  |  cost
----+--------+-------
1   |  2012  |  2.01
2   |  2013  |  2.02
3   |  2013  |  2.03

I want to get:

id  |  year  |  cost
----+--------+-------
1   |  2012  |  1.01
2   |  2013  |  1.02
2   |  2013  |  1.03
3   |  2013  |  2.03

This is obviously wrong, but I tried:

SELECT
  T1.id,
  T1.year,
  T1.cost
FROM employee_cost AS T1
  INNER JOIN (select
        employee_cost_historic.id,
        employee_cost_historic.year,
        employee_cost_historic.cost
          from employee_cost_historic) AS T2
    ON T1.id = T2.id
      AND T1.year = T2.year
  • 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-18T02:31:53+00:00Added an answer on June 18, 2026 at 2:31 am

    You can do this with a union all:

    select id, YEAR, cost
    from t2
    union all
    select id, YEAR, cost
    from t2
    where not exists (select 1 from t1 where t1.id = t2.id and t1.year = t2.year)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to merge two tables: Both have a primary key-column date, but with
I want to LEFT JOIN two tables with the same column name. I have
I have two tables: <class name=Content table=language_content lazy=false> <composite-id> <key-property name=contentID column=contentID/> <key-property name=languageID
I have two or more tables which hold values under the same column name.
i have two data tables with the same structure the first one has one
I have two fields in separate linked tables with the same data, but different
I have two tables, with a same column named user_name , saying table_a ,
I have two tables: Dictionary, Names. Both tables have large number of rows (like
I have two tables, where only one column is the same. I am trying
I have two datatables with same identity column. I want to update one column

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.