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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:52:34+00:00 2026-05-19T00:52:34+00:00

Assuming I have a table like this one: CREATE TABLE user_delegates ( [id] INT

  • 0

Assuming I have a table like this one:

CREATE TABLE user_delegates (
    [id] INT IDENTITY(1,1) NOT NULL,
    [user_from] VARCHAR(10) NOT NULL,
    [user_to] VARCHAR(10) NOT NULL,
    CONSTRAINT [PK_user_delegates] PRIMARY KEY CLUSTERED ([id] ASC),
    CONSTRAINT [UK_user_delegates] UNIQUE ([user_from] ASC)
)

So an user A has to right to delegate her system access to another user B. When she does that, she won’t be able to access the system anymore – user B will have to “break” that delegation before she is able to use the system again…

BUT also consider that, if user B delegates access to user C, user C will also start impersonating user A, and so on.

(I know this seems to be a security nightmare – please let’s just forget about that, OK? :-))

Also consider those records:

INSERT INTO user_delegates([user_from], [user_to]) values ('ANTHONY', 'JOHN')
INSERT INTO user_delegates([user_from], [user_to]) values ('JOHN', 'JOHN')
INSERT INTO user_delegates([user_from], [user_to]) values ('KARL', 'JOSHUA')
INSERT INTO user_delegates([user_from], [user_to]) values ('JOSHUA', 'PIOTR')
INSERT INTO user_delegates([user_from], [user_to]) values ('PIOTR', 'HANS')

So what I need is finding the last (which means the active) delegation for each user.

I have come to a solution that I’ve decided to not show here (unless everybody ignores me, which is always a possibility). All I can say is that it is a somewhat long answer, and it surely seems like using a cannon to kill a flea…

But how would you do that? Consider any relevant SQL Server extension available, and notice we’re looking for an answer that is both elegant and with a good performance…

BTW, this is the expected result set:

id          user_from  user_to   
----------- ---------- ----------
1           ANTHONY    JOHN      
2           JOHN       JOHN      
3           KARL       HANS      
4           JOSHUA     HANS      
5           PIOTR      HANS      

(5 row(s) affected)

And thanks in advance!

  • 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-19T00:52:35+00:00Added an answer on May 19, 2026 at 12:52 am
    WITH    q (user_initial, user_from, user_to, link) AS
            (
            SELECT  user_id, user_id, user_id, link
            FROM    users
            UNION ALL
            SELECT  user_initial, q.user_to, ud.user_to, link + 1
            FROM    q
            JOIN    user_delegates ud
            ON      ud.user_from = q.user_to
            )
    SELECT  *
    FROM    (
            SELECT  *, ROW_NUMBER() OVER (PARTITION BY user_initial ORDER BY link DESC) rn
            FROM    q
            )
    WHERE   rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hej, assuming I have a code that looks like this: List<User> userList = GetUserByName
Assuming I have a class A as follows: class A{ int id; int getId(){};
It's my understanding that nulls are not indexable in DB2, so assuming we have
Let's say I have a simple stored procedure that looks like this (note: this
Imagine the following tables: create table boxes( id int, name text, ...); create table
I have title (varchar), description (text), keywords (varchar) fields in my mysql table. I
In SQL Server assuming one has columns that need to have the same data
I have a legacy DB dump file which starts off something like this: ^C^@&D
Assuming I have three tables : TableA (key, value) TableB (key, value) TableC (key,
Let's say I have two tables: Report Comment And assuming I have a database

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.