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

x
?
Solved

Changing a query to decode numerical reference data

Posted on 2008-06-19
2
Medium Priority
?
241 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

604 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