Solved

Query to show username in place of ID

Posted on 2013-06-20
4
168 Views
Last Modified: 2013-06-21
Hello


We have made the following query:

SELECT [HDTicket_Id]
	  ,[HDTicket_Identifier]
	  ,[HDTicket_Subject]
	  ,[HDTicket_Assignee_Benutzer_Id]
      ,[HDTicket_Customer_Benutzer_Id]
      ,[HDTicket_Created]
      ,[HDTicket_Modified] 
      ,[HDTicket_Closed]
      ,[HDStatus_Name]
FROM [macom].[dbo].[dt_HDTicket] 
Join [macom].[dbo].[dt_HDStatus]  
on HDTicket_HDStatus_id=HDStatus_ID
 
Join [macom].[dbo].[Benutzer]
on HDTicket_Assignee_Benutzer_Id=id

where HDTicket_IsClosed = '1' And HDTicket_Closed >= Dateadd(WW, -1, getdate()) and HDStatus_HDProject_id = '7' and HDStatus_Name <> 'Duplicate'

Open in new window


The 2 fields "HDTicket_Assignee_Benutzer_Id" and "HDTicket_Customer_Benutzer_Id" are linked to the table dbo.benutzer's filed ID.

We want to display columns of "HDTicket_Assignee_Benutzer_Id" and "HDTicket_Customer_Benutzer_Id" to display the filed "usersname" from the table dbo.benutzer


Thanks in advanced for the help
0
Comment
Question by:macomsupport
  • 2
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 39262837
Hi.

Is the issue that you have two different dbo.benutzer.ID values? If yes, you will want to have two JOINs. I will show with INNER JOIN, which is appropriate if you must have two valid ID values in HDTicket. If there is a chance one or both of the fields are NULL (i.e., no match), you should use a LEFT JOIN.

SELECT b_c.usersname AS Customer, b_a.usersname AS Assignee
FROM [macom].[dbo].[dt_HDTicket] t
JOIN [macom].[dbo].[Benutzer] b_a ON t.HDTicket_Assignee_Benutzer_Id=b_a.id
JOIN [macom].[dbo].[Benutzer] b_c ON t.HDTicket_Assignee_Benutzer_Id=b_c.id

I hope that helps! The key is the alias to remove ambiguity between the usersname values.
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 39262947
I think the intention above was as follows:

SELECT b_c.usersname AS Customer, b_a.usersname AS Assignee
FROM [macom].[dbo].[dt_HDTicket] t
JOIN [macom].[dbo].[Benutzer] b_a ON t.HDTicket_Assignee_Benutzer_Id=b_a.id
JOIN [macom].[dbo].[Benutzer] b_c ON t.HDTicket_Customer_Benutzer_Id=b_c.id

aliases differ in the following, but the intent is exactly the same:
SELECT -- please add the correct aliases to all fields referenced
      [HDTicket_Id]
    , [HDTicket_Identifier]
    , [HDTicket_Subject]
    , t.[HDTicket_Assignee_Benutzer_Id]
    , t.[HDTicket_Customer_Benutzer_Id]
    , ag.usersname AS Assignee
    , cu.usersname AS Customer
    , [HDTicket_Created]
    , [HDTicket_Modified]
    , [HDTicket_Closed]
    , [HDStatus_Name]
FROM [macom].[dbo].[dt_HDTicket] as t
INNER JOIN [macom].[dbo].[dt_HDStatus] as st ON t.HDTicket_HDStatus_id = st.HDStatus_ID
INNER JOIN [macom].[dbo].[Benutzer] as ag    ON t.HDTicket_Assignee_Benutzer_Id = ag.id
INNER JOIN [macom].[dbo].[Benutzer] as cu    ON t.HDTicket_Customer_Benutzer_Id = cu.id
WHERE HDTicket_IsClosed = '1'
    AND HDTicket_Closed >= Dateadd(WW, - 1, getdate())
    AND HDStatus_HDProject_id = '7'
    AND HDStatus_Name <> 'Duplicate'

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 39262973
Thanks, @PortletPaul! That was a copy and paste error for sure.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39263025
:) it happens
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question