Solved

Query to show username in place of ID

Posted on 2013-06-20
4
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 60

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 49

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 60

Expert Comment

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

Expert Comment

by:PortletPaul
ID: 39263025
:) it happens
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

689 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