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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:06:07+00:00 2026-06-13T08:06:07+00:00

Background I have a table which has six columns. The first three columns create

  • 0

Background

I have a table which has six columns. The first three columns create the pk. I’m tasked with removing one of the pk columns.

I selected (using distinct) the data into a temp table (excluding the third column), and tried inserting all of that data back into the original table with the third column being ’11’ for every row as this is what I was instructed to do. (this column is going to be removed by a DBA after I do this)

However, when I went to insert this data back into the original table I get a pk constraint error. (shocking, I know)

The other three columns are just date columns, so the distinct select didn’t create a unique pk for each record. What I’m trying to achieve is just calling a distinct on the first two columns, and then just arbitrarily selecting the three other columns as it doesn’t matter which dates I choose (at least not on dev).

What I’ve tried

I found the following post which seems to achieve what I want:

How do I (or can I) SELECT DISTINCT on multiple columns?

I tried the answers from both Joel,and Erwin.

Attempt 1:

However, with Joels answer the set returned is too large – the inner join isn’t doing what I thought it would do. Selecting distinct col1 and col2 there are 400 columns returned, however when I use his solution 600 rows are returned. I checked the data and in fact there were duplicate pk’s. Here is my attempt at duplicating Joels answer:

select a.emp_no, 
        a.eec_planning_unit_cde, 
        '11' as area, create_dte, 
        create_by_emp_no, modify_dte,
        modify_by_emp_no
from tempdb.guest.temp_part_time_evaluator b
inner join
(
    select emp_no, eec_planning_unit_cde
    from tempdb.guest.temp_part_time_evaluator
    group by emp_no, eec_planning_unit_cde
) a
ON b.emp_no = a.emp_no AND b.eec_planning_unit_cde = a.eec_planning_unit_cde

Now, if I execute just the inner select statement 400 rows are returned. If I select the whole query 600 rows are returned? Isn’t inner join supposed to only show the intersection of the two sets?

Attempt 2:

I also tried the answer from Erwin. This one has a syntax error and I’m having trouble googling the spec on the where clause (specifically, the trick he is using with (emp_no, eec_planning_unit_cde))

Here is the attempt:

select emp_no, 
    eec_planning_unit_cde, 
    '11' as area, create_dte, 
    create_by_emp_no, 
    modify_dte,
    modify_by_emp_no
where (emp_no, eec_planning_unit_cde) IN
(
    select emp_no, eec_planning_unit_cde
    from tempdb.guest.temp_part_time_evaluator
    group by emp_no, eec_planning_unit_cde
)

Now, I realize that the post I referenced is for postgresql. Doesn’t T-SQL have something similar? Trying to google parenthesis isn’t working too well.

Overview of Questions:

  1. Why doesn’t inner join return an intersection of two sets? From googling this is what I thought it was supposed to do
  2. Is there another way to achieve the same method that I was trying in attempt 2 in t-sql?
  3. It doesn’t matter to me which one of these I use, or if I use another solution… how should I go about this?
  • 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-13T08:06:08+00:00Added an answer on June 13, 2026 at 8:06 am

    A select distinct will be based on all columns so it does not guarantee the first two to be distinct

    select pk1, pk2, '11', max(c1), max(c2), max(c3) 
    from table 
    group by pk1, pk2 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table cell which has class cellActive. Which has following defination .cellActive{background:#DDDDDD} Now
Background: I have a database table which is for received_msgs The table has two
I have a table which has a background image(sprite). The td of the table
A little background information: I have a table called table_a , which has 12
I have a large table which has a background-color on each td. I also
I have a table which is zebra-striped (different background color for each odd rows).
Background I have a dimension table that has a single record for each day.
I have given a table background image using css background-image property. The cells are
Background I have two lists, the first is items which contains around 250 tuples,
I have a table which has textboxes in its cells. All these textboxes are

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.