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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:58:10+00:00 2026-06-05T14:58:10+00:00

code updated: Added [score] column to #left table. The join must pull student name

  • 0

code updated:
Added [score] column to #left table. The join must pull student name and score from #left table. Any one score is fine but it should not pull duplicate student_id. I am thinking cursor can do it?


I am running this report. The problem is similar to the the script that I created. To sum up, I have two tables lets call it #left and #right. The #right table is the key one which contains the amount. But the #left table contains the agent name which I must retrieve too.

create table #left (
id int not null primary key identity,
student_id int, name varchar(20),score int)

insert into #left values( 1, 'James',10)
insert into #left values( 2, 'Parker',20)
insert into #left values( 3, 'Smith',30)
insert into #left values( 4, 'Rog',40)
insert into #left values( 1, 'James',50)
insert into #left values( 2, 'Parker',60)

create table #right (
id int not null primary key identity,
student_id int,
amount decimal(5,2)
)

insert into #right values (1,5.25)
insert into #right values (3,7.25)
insert into #right values (4,3.25)
insert into #right values (1,5.25)

The problem here is I want to join the two tables on student_id but as you can see #left table contains James and Parker 2 times with the same id. Lets assume this was database mistake.

A similar problem can exists in the #right table. But that is not a mistake. It simply means the #left student was associated with two payments (even if the two payments are the same).

I want to remove dups from #left table and keep the dups in the #right table.

I came up with this query, which works

/* query 1 */
select student_id, amount from #right R
where exists
(
select student_id from #left L
where R.student_id = L.student_id
);

But the problem here is I also need to pull the student name from the other table. Is there a way to do it. It can be any syntax but efficient query is desirable.

  • 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-05T14:58:11+00:00Added an answer on June 5, 2026 at 2:58 pm

    This should work:

    Solution to the original problem

    Where #left table looked like this

    create table #left (
    id int not null primary key identity,
    student_id int, name varchar(20))
    
    insert into #left values( 1, 'James')
    insert into #left values( 2, 'Parker')
    insert into #left values( 3, 'Smith')
    insert into #left values( 4, 'Rog')
    insert into #left values( 1, 'James')
    insert into #left values( 2, 'Parker')
    

    Query: 1

    SELECT A.student_id, A.amount, B.name
    FROM #right A
    LEFT JOIN (SELECT DISTINCT student_id, name FROM #left) B
    ON A.student_id = B.student_id
    

    UPDATED to the modified criteria Well, if any score is fine, then you can do this:

    Query: 2

    SELECT A.student_id, A.amount, B.name, B.score
    FROM #right A
    LEFT JOIN ( SELECT student_id, name, MIN(score) score
                FROM #left
                group by student_id, name) B
    ON A.student_id = B.student_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I updated my code with string dates created with new Date and added back
UPDATE 1 ADDED UPDATED CODE I have a django template on app engine. Currently
In my CMS I've added this code <div><?php include(my_contact_form.php) ?></div> which updates to a
I have updated code for ActiveX functionality which already installed on client(in their Windows
I ran some CUDA code that updated an array of floats. I have a
EDIT : Scroll down to see the updated code. I would like to build
I updated my code like Andro wrote about to me: public class ZiiziiActivity extends
I've updated some code to use the Ajax Control toolkit 0911 beta and for
I've updated my code here. I've put the range inside the unavailableDays function which
My new code with the updated changes. Clicking the button after inserting a number

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.