EJF
EJF

Reputation: 461

SQL: Select the same column twice in one query?

I'm currently working on a query where I need to pull a list of items, as well as the date they were entered into the system and the name and user ID of the person who entered them.

My Items table contains the columns CreateDate, CreatedBy (this is the person's user ID), LastChanged (this is a date), and LastChangedBy (also a user ID, being as the person who entered the item isn't always the same person who created it).

I have the Items table joined with a User table so I can pull the first and last name of the users who match the CreatedBy and LastChangedBy user IDs. The way I have the query written now, I've concatenated the two fields so they end up displayed as "LastName, FirstName" in a single column. I also want to only pull results for a certain user, and this user just needs to be the last person who changed the item - it doesn't matter whether or not they created it.

My query looks something like this:

SELECT 
    i.ItemName, i.CreateDate, 
    (u.LastName + ', ' + u.FirstName) as [Created By Name], 
    i.CreatedBy, i.LastChanged, 
    (u.LastName + ', ' + u.FirstName) as [Last Changed By Name],
    i.LastChangedBy
FROM 
    Items i 
JOIN 
    Users u ON i.CreatedBy = u.UserID
WHERE 
    i.LastChangedBy = 0001;

My issue though is that since I have the exact same piece of code to pull the user's name both times, it's pulling the same name regardless of whether the user IDs are different in the CreatedBy and LastChangedBy columns. So I end up with results that look like this:

ItemName     CreateDate     Created By Name    CreatedBy    LastChanged    Last Changed By Name    LastChangedBy

Item A       12/01/2015     Smith, John        0001         12/03/2015     Smith, John             0001
Item B       12/02/2015     Smith, John        0001         12/04/2015     Smith, John             0001
Item C       12/02/2015     Doe, Jane          1002         12/05/2015     Doe, Jane               0001

So even though John Smith was the last person to change Item C, it's still displaying Jane Doe's name there because (I assume) the strings of code to display the user's name is the same in both instances and it's just pulling the same name both times.

Is there a way to pull the first and last names from the User table twice in the same query but make sure they correspond with the correct user IDs?

Upvotes: 1

Views: 1789

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271231

I think you are missing a join in your query:

SELECT i.ItemName, i.CreateDate,
       (uc.LastName + ', ' + uc.FirstName) as [Created By Name], 
       i.CreatedBy, i.LastChanged,
       (ucb.LastName + ', ' + ucb.FirstName) as [Last Changed By Name],
       i.LastChangedBy
FROM Items i LEFT JOIN
     Users uc
     ON i.CreatedBy = uc.UserID LEFT JOIN
     Users ucb
     ON i.LastChangedBy = ucb.UserId
WHERE i.LastChangedBy = 0001;

You need to join twice to get both users.

Upvotes: 3

Related Questions