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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:23:21+00:00 2026-05-19T17:23:21+00:00

I am looking to select the id of a record from @table1 when that

  • 0

I am looking to select the id of a record from @table1 when that record is the only record in that table and is not currently linked in @t1Tot2 to a particular id from another table.

The following query below works, but I am wondering if there is a better way. It is setup to currently to return 55 the id of the only record added to table @table1. Inserting another record into @table1 would cause it to return no records ( good ), and linking @t2id in @t1Tot2 would make it return none as well ( good ). Is there a better way? Thanks.

DECLARE @t2id INT
SET @t2id = 1 --Record to link to
DECLARE @table1 TABLE
(
t1id int
)
DECLARE @t1Tot2 TABLE
(
t1id INT,
t2id int
)
INSERT INTO @table1
( t1id )
VALUES  ( 55  -- t1id - int
)

--Will cause the query below to return no records because of having more than 1 record to be linked to
--  INSERT INTO @table1
--        ( t1id )
--VALUES  ( 2  -- t1id - int
--          )    

--Will cause the query below to return no records because of already being linked to the t1id
--INSERT INTO @t1Tot2
--( t1id, t2id )
--VALUES  ( 55, -- t1id - int
--@t2id  -- t2id - int
--)    

SELECT MAX(a.t1id)
FROM @table1 a
LEFT JOIN @t1Tot2 b ON a.t1id = b.t1id AND b.t2id = 1
HAVING COUNT(1) = 1 AND SUM( CASE WHEN b.t2id IS NULL THEN 0 ELSE 1 END ) = 0
  • 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-05-19T17:23:22+00:00Added an answer on May 19, 2026 at 5:23 pm
    --declare @table1 table (t1id int)
    --declare @t1Tot2 table (t1id int)
    
    select a.t1id
    from @table1 a
    where (select count(*) from (select top 2 * from @table1) x) = 1
    and not exists (
        select * from @t1Tot2 b
        where b.t1id = a.t1id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking at fast ways to select a random row from a table
I have an SQL query that I am looking to optimize. SELECT * FROM
I'm looking for the best way to select numbers directly from an in clause.
I am looking for a searchable multiple select javascript widget that can handle filtering
Looking at the document, the goal is to select the second cell from the
I looking for a script that will enable me to select an area in
I'm looking for something like this: SELECT german_subsidiaries.*, customers.* FROM german_subsidiaries INNER JOIN customers
I'm currently looking at ways to allow people to select multiple files at once
Hey all, I am looking for a way to query my database table only
I'm looking to do a SELECT with an ORDER BY on a VARCHAR(200) field.

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.