Solved

Changing a query to decode numerical reference data

Posted on 2008-06-19
2
237 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

820 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