I am trying to figure my sql select statement

David Fetrow 1 Reputation point
2020-11-18T13:13:52.367+00:00

I have 2 tables
employee
id, employeeID

Organization
id, timekeeper, Alt1Timekeeper, Alt2Timekeeper

The id in employee in the key in all timekeeper fields

So...

Employee
1, 3456
2, 5689

Organization

1, 1, 23,45
2,33,1, 66
3, 67, 55, 1
4, 57, 77, 88, 54
5, 1, 62, 74,99

I have the employee ID.
Notice that employee 3456 is either a timekeeper or alt in either orgs 1,2,3,5

I want to do a select statement that pulls back all the org record ids that employee 1 is in????

Developer technologies | Transact-SQL
{count} votes

3 answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,586 Reputation points
    2020-11-18T15:34:00.32+00:00

    Hi @David Fetrow ,

    Please try the following solution. It is using XQuery and Quantified Expressions.
    And it is generic, meaning if the Organization table has more columns to check like AltNTimekeeper, it will work without any T-SQL modification.

    SQL

    -- DDL and sample data population, start  
    DECLARE @employee TABLE (id INT PRIMARY KEY, employeeID INT);  
    INSERT INTO @employee (id, employeeID) VALUES  
    (1, 3456),  
    (2, 5689);  
    
    DECLARE @Organization TABLE (id INT PRIMARY KEY, timekeeper INT, Alt1Timekeeper INT, Alt2Timekeeper INT);  
    INSERT INTO @Organization (id, timekeeper, Alt1Timekeeper, Alt2Timekeeper) VALUES  
    (1, 1, 23,45),  
    (2,33,1, 66),  
    (3, 67, 55, 1),  
    (4, 57, 77, 88),  
    (5, 1, 62, 74);  
    -- DDL and sample data population, end  
      
    DECLARE @employeeID INT = 3456;  
    DECLARE @id INT = (SELECT id FROM @employee WHERE employeeID = @employeeID);  
      
    ;WITH rs AS  
    (  
    SELECT *, (  
     SELECT * FROM @Organization AS c  
     WHERE c.id = p.id  
     FOR XML PATH('r'), TYPE, ROOT('root')).value('some $r in /root/r/*[local-name()!=("id")]/text()  
                satisfies $r = sql:variable("@id")', 'BIT') AS Result  
     FROM @Organization AS p  
    )  
    SELECT * FROM rs  
    WHERE Result = 1;  
    
    1 person found this answer helpful.

  2. Guoxiong 8,206 Reputation points
    2020-11-18T15:19:34.46+00:00
    DECLARE @employeeID int = 3456;
    DECLARE @id int;
    SELECT @id = id FROM Employee WHERE EmployeeID = @employeeID;
    SELECT * 
    FROM Organization
    WHERE id = @id OR timekeeper = @id OR Alt1Timekeeper = @id OR Alt2Timekeeper = @id;
    
    0 comments No comments

  3. EchoLiu-MSFT 14,621 Reputation points
    2020-11-19T02:10:53.157+00:00

    Hi @David Fetrow ,

    According to my understanding, I have tried the following, if this does not solve your problem,please share us your table structure (CREATE TABLE …) and some sample data(INSERT INTO …)
    along with your expected result? So that we’ll get a right direction and make some test.

    Please refer to:

        declare @employee table(id int, employeeID int)  
        insert into @employee values(1,3456),(2,5689)   
        declare @Organization table(id int, timekeeper int, Alt1Timekeeper int, Alt2Timekeeper int)  
        insert into @Organization values(1, 1, 23,45),(2,33,1, 66),(3, 67, 55, 1)  
                                       ,(4, 57, 77, 88),(5, 1, 62, 74)   
        ;with cte  
        as(select e.id,e.employeeID,o.id id2,o.timekeeper,o.Alt1Timekeeper,o.Alt2Timekeeper from @employee e  
        full join @Organization o  
        on e.id=o.id)  
          
        select id2 id,timekeeper,Alt1Timekeeper,Alt2Timekeeper from cte  
        where timekeeper=1   
        union all  
        select id2 id,timekeeper,Alt1Timekeeper,Alt2Timekeeper from cte  
        where Alt1Timekeeper=1  
        union all  
        select id2 id,timekeeper,Alt1Timekeeper,Alt2Timekeeper from cte  
        where Alt2Timekeeper=1  
    

    40952-image.png

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Hot issues November--What can I do if my transaction log is full?
    Hot issues November--How to convert Profiler trace into a SQL Server table

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.