Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query to show username in place of ID

Posted on 2013-06-20
4
Medium Priority
?
175 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 1000 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 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

610 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