Solved

Changing a query to decode numerical reference data

Posted on 2008-06-19
2
239 Views
Last Modified: 2010-03-19
Have the following query :

SELECT DISTINCT
cc.ClientID,
cc.PriorityKey,
cc.ReliabilityKey,
pty.ClientFullName,
s.Forename,
s.Surname,
rl.DataLookupKey AS RoleId,        
rl.DataDisplayName AS RoleName
            
FROM ClientDetails cc
INNER JOIN CaseClients cs
           ON cs.CaseClientsID = cc.ClientID
                
INNER JOIN ClientNames pty
           ON pty.ClientNameID = cs.CaseClientsID
INNER JOIN StaffDetails sd
           ON sd.ClientID = cc.ClientID
           
INNER JOIN UserList s ON sd.UserListID = s.UserListID  
INNER JOIN ReferenceData rl ON sd.SecurityRoleKey = rl.DataLookupKey              
      
Above query works OK (backend database is SQL 2005 just migrated from SQL 2000) - problem is that the values retrieved for PriorityKey and ReliabilityKey are numerics and that has been fine for our application up until now.

The numeric values for these two fields can be decoded by looking at DataLookupKey on the ReferenceData table (matching on DataLookupKey) and taking the text description from DataDisplayName column on the ReferenceData table.

We now need to amend the above query to show the text descriptions for PriorityKey and ReliabilityKey as well as the existing numeric values, so how should I amend the above query to do that ?
(As an aside given that we have now moved to SQL 2005, I wasn't sure whether the query would
benefit from using a CTE/Common Table Expression ?) ...



0
Comment
Question by:raymurphy
[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 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21820942
If I understood the problem correctly, you need a second JOIN to ReferenceData:
SELECT DISTINCT
cc.ClientID, 
cc.PriorityKey,
cc.ReliabilityKey,
pty.ClientFullName,
s.Forename,
s.Surname,
rl.DataLookupKey AS RoleId,        
rl.DataDisplayName AS RoleName
r2.DataLookupKey AS RoleId2,        
r2.DataDisplayName AS RoleName2            
FROM ClientDetails cc 
INNER JOIN CaseClients cs 
           ON cs.CaseClientsID = cc.ClientID 
                 
INNER JOIN ClientNames pty 
           ON pty.ClientNameID = cs.CaseClientsID
INNER JOIN StaffDetails sd
           ON sd.ClientID = cc.ClientID
            
INNER JOIN UserList s ON sd.UserListID = s.UserListID  
INNER JOIN ReferenceData rl ON sd.SecurityRoleKey = rl.DataLookupKey               
INNER JOIN ReferenceData r2 ON sd.ReliabilityKey = r2.DataLookupKey              

Open in new window

0
 

Author Comment

by:raymurphy
ID: 21822161
Works fine - thanks (hadn't realised it was simple as adding another join ...)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

724 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