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

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

The Archive Base Latest Questions

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

Please consider the following SQL. declare @t1 table(site int, id int, name varchar(2)) declare

  • 0

Please consider the following SQL.

declare @t1 table(site int, id int, name varchar(2))
declare @t2 table(site int, id int, mark int)

insert into @t1
select 1,1,'A'
union select 1,2,'B'
union select 1,3,'C'
union select 2,2,'D'
union select 2,3,'C'

insert into @t2
select 1,1,10
union select 1,2,20
union select 0,3,30
union select 1,3,40
union select 2,3,40
union select 2,3,40

select distinct a.site, a.id,a.name,b.mark
from @t1 a 
inner join @t2 b
on (a.site =b.site or b.site = 0) and a.id = b.id
where a.site=1

It produces the following result

site id name mark
----------------------------
1    1    A    10
1    2    B    20
1    3    C    30
1    3    C    40

It’s correct.

But I want a person’s data exactly once. The SQL should first check whether there is an entry for a person in @t2 for a specific site. If entry is found, then use it. If not, the mark of that person will be the person’s mark who has the same name in site 0.

In this case, I want the result as follows.

site id name mark
----------------------------
1    1    A    10
1    2    B    20
1    3    C    40

But if (1,3,40) isn’t in @t2, The result should be as follows.

site id name mark
----------------------------
1    1    A    10
1    2    B    20
1    3    C    30

How can I do this?
I can do it using Common Table Expression.
So please provide me a faster way.
I’ll run it on about 100 millions rows.

  • 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-15T23:12:44+00:00Added an answer on June 15, 2026 at 11:12 pm

    You can roll all of the conditions into the on clause:

    declare @target_site as Int = 1
    select distinct a.site, a.id, a.name, b.mark
      from @t1 as a inner join
        @t2 as b on a.site = @target_site and a.id = b.id and
          ( a.site = b.site or ( b.site = 0 and not exists ( select 42 from @t2 where site = @target_site and id = a.id ) ) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following: declare @abbrev varchar(20); set @abbrev=''; select pk_term into #t2 from
Please consider the following SQL Server table and stored procedure. create table customers(cusnum int,
Please consider the following SQL. I'm trying to create a table that references another.
Please consider following schema CREATE table articles ( id Int UNSIGNED NOT NULL AUTO_INCREMENT,
Please Consider the following table : As you can see the Name column has
I am using SQL Server 2005. Please consider the following query: DECLARE @SearchTerm NVARCHAR(4)
Please consider the following HTML: <input type='text' id='name'/> <option id='option'> <select value='1'>hi</select> <select value='2'>bye</select>
Consider the following SQL statement: DECLARE @CustomerId INT = 48; DECLARE @CurrentYear INT =
Please consider the following two entities: @Entity @Table(name = PROFILE) public class Profile extends
Please consider the following simple use case: public class Foo { public virtual int

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.