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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:11:36+00:00 2026-05-19T11:11:36+00:00

I am trying to do quite a straight forward TSQL link table but I

  • 0

I am trying to do quite a straight forward TSQL link table but I am falling at the first hurdle, basically I want a two way link.

For example ….

If Contact 1 adds contact 2, then contact 2 is connected to contact 1 (and vice versa) the idea being that the query recognises they are paired up.

Table Example

RelatedDataID  UniqueID   Related_UniqueID  
-------------------------------------------    
1              AA          BB   
2              CC          DD                     
3

Users Table

UserID  UniqueID   Username
----------------
1        AA       Bob    
2        BB       Fred    
3        CC       Charlie
4        DD       Billy

So basically when I run a query With UniqueID “AA” it will return

Username     RelatedID
------------------------    
Bob          1    
Fred         1    
Charlie      0
Billy        0

But also when I run it on UniqueID “CC” it should return

Username   RelatedID
---------------------
Bob        0    
Fred       0    
Charlie    2
Billy      2

Does anyone know how I can achieve this? My current stored procedure seems to only bring back ones that are linked, and not the ones that are not linked. I need it to bring back a full list of all users, but to have the RelatedID come back as either a 0 or the RelatedDataID.

The UniqueID’s are GUIDs.

Here my TSQL statement.

ALTER PROCEDURE sp_Test
 @CompanyID int,
 @UniqueID varchar(36),
 @PersonTypeID int
AS
BEGIN
 SET NOCOUNT ON;

 SELECT
  First_Name + ' ' + Last_Name AS Full_Name,
  ISNULL(RelatedDataID,0) AS RelatedDataID
 FROM
  Users
 LEFT JOIN
  Related_Data
 ON
  Users.UniqueID = Related_Data.UniqueID
 WHERE
  Users.PersonTypeID = @PersonTypeID
 AND
  Users.Deleted = '0'
 AND
  ((ISNULL(Related_Data.UniqueID,'') = '') OR (Related_Data.UniqueID = @UniqueID OR Related_Data.Related_UniqueID = @UniqueID))
  • 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-19T11:11:37+00:00Added an answer on May 19, 2026 at 11:11 am

    (UPDATE) Given that you mentioned in comments that the UniqueId columns are guids and you want a zero to return when no relations, we have to case the guids to varchar. It is also not clear what the expected output should be if the a given user has multiple relations. This query will choose a relation on User.UniqueID = RelatedData.UniqueId over a relation on User.UniqueId = RelatedData.Related_UniqueId but will only return one row per user.

    Select U.username
        , U.FirstName + ' ' + U.LastName As Full_Name
    , Case 
        When RD.Related_UniqueID Is Not Null Then RD.RelatedDataId
        When RD1.UniqueId Is Not Null Then RD1.RelatedDataId
        Else 0
        End As RelatedID
    From Users As U
        Left Join Related_Data As RD
            On RD.UniqueID = U.UniqueId
                And (
                    RD.Related_UniqueID = @UniqueId
                    Or RD.UniqueId = @UniqueId
                    )
        Left Join Related_Data As RD1
            On RD1.Related_UniqueID = U.UniqueId
                And RD1.UniqueID = @UniqueId
    Where U.Deleted = '0'
        And U.PersonTypeId = @PersonTypeId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm quite new with php. i think this is quite straight forward but i'm
I'm trying to make it working for quite some time,but just can't seem to
The case is quite straight forward (in my mind). The client has a native
What I'm trying to do is quite simple but as a beginner I'm getting
We’re trying to come up with something approaching a simple and straight-forward model for
While I am trying to learn WCF, and it seems straight-forward enough, I came
Yeah, the title can scare babies, but it's actually quite straightforward. I am trying
The problem I'm trying to solve is fairly straight forward, I'm using Microsoft.Phone.Controls and
I have been trying for quite a while to check if submitted links are
I am trying to accomplish something that seemed quite simple... I have 3 div

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.