I’m struggling with the right type of conditional select statement here, even after searching.
I have a table with users. Some are owners, some are just guests, like so:
TABLE A
UserID | UserType | Directory | RegisteredToID | Name
1 | Guest | | 3 | Bob
2 | Guest | | 3 | Susan
3 | Owner | abc | (null) | Charles
4 | Guest | | 20 | Hugh
etc...
I’m trying to get one query statement that results the Bob, Susan, and Charles records with the directory name showing for each, like so:
Name | Directory
Bob | abc
Susan | abc
Charles | abc
In other words, Bob and Susan are guests of Charles, but Charles’ record contains the directory name I want. Hope my question makes sense?
EDIT: I should have mentioned this twist….I won’t know the directory name, just a userID (e.g. 1 for Bob) and am trying to retrieve the correct directory name to associate for each user, whether they are a guest or not. Only owner records have a directory name filled in.
You need to join the table on itself and decide with
CASEwhich Directory to select.Out of my head (don’t have access to a SQL Server at the moment):