Solved

How to calculate a field pulling data from a different table based on a value in the main table

Posted on 2013-06-25
3
330 Views
Last Modified: 2013-06-25
I have a complicated SQL that joins multiple tables.  The main table is a history table that contains IDs that link to other tables.  One of the IDs is for Manager Name which is pulled from 2 different tables depending on the value of the ManagerID in the History table.  If it is positive the Manager Name is created by using the LastName || ', ' || FirstName from the Employee Table.  If the ID is negative, the Manager Name needs to be pulled from another table.  This is the part I need help with.  I don't know how to create the Manager Name based on the ID in the History table.  Here is my SQL:

SELECT EffectiveDate, ReasonDesc, TitleDesc, LastName || ', ' || FirstName As ManagerName, Division, Department, GroupDesc, FunctionDesc, LocationDesc, SubLocationDesc
FROM EMP_OWNER.EMP_EmployeeHistory 
LEFT JOIN EMP_OWNER.EMP_Title ON EMP_Title.TitleID = EMP_EmployeeHistory.TitleID
LEFT JOIN EMP_OWNER.EMP_Employee ON EMP_Employee.EmployeeID = EMP_EmployeeHistory.ManagerSupervisorID
LEFT JOIN EMP_OWNER.EMP_OpenTeam ON EMP_OpenTeam.OpenTeamID = EMP_EmployeeHistory.ManagerSupervisorID
LEFT JOIN EMP_OWNER.EMP_Division ON EMP_Division.DivisionID = EMP_EmployeeHistory.DivisionID
LEFT JOIN EMP_OWNER.EMP_Department ON EMP_Department.DepartmentID = EMP_EmployeeHistory.DepartmentID
LEFT JOIN EMP_OWNER.EMP_Group ON EMP_Group.GroupID = EMP_EmployeeHistory.GroupID
LEFT JOIN EMP_OWNER.EMP_Function ON EMP_Function.FunctionID = EMP_EmployeeHistory.FunctionID
LEFT JOIN EMP_OWNER.EMP_Location ON EMP_Location.LocationID = EMP_EmployeeHistory.LocationID
LEFT JOIN EMP_OWNER.EMP_SubLocation ON EMP_SubLocation.SubLocationID = EMP_EmployeeHistory.SubLocationID
LEFT JOIN EMP_OWNER.EMP_Reason ON EMP_Reason.ReasonID = EMP_EmployeeHistory.ReasonID
WHERE EMP_EmployeeHistory.EmployeeID = 776
ORDER BY EMP_EmployeeHistory.EmployeeHistoryID DESC

Open in new window


How do I pull the Manager Name from the Employee Table when ManagerSupervisorID is positive and from the OpenTeam Table when the ManagerSupervisorID is negative?  Any help is greatly appreciated.
0
Comment
Question by:dyarosh
3 Comments
 
LVL 15

Accepted Solution

by:
Walter Ritzel earned 500 total points
Comment Utility
Please try this:
SELECT EffectiveDate, ReasonDesc, TitleDesc, case when EMP_EmployeeHistory.ManagerSupervisorID > 0 then EMP_Employee.LastName || ', ' || EMP_Employee.FirstName else  EMP_OpenTeam.LastName || ', ' || EMP_OpenTeam.FirstName end As ManagerName, Division, Department, GroupDesc, FunctionDesc, LocationDesc, SubLocationDesc
FROM EMP_OWNER.EMP_EmployeeHistory 
LEFT JOIN EMP_OWNER.EMP_Title ON EMP_Title.TitleID = EMP_EmployeeHistory.TitleID
LEFT JOIN EMP_OWNER.EMP_Employee ON EMP_Employee.EmployeeID = EMP_EmployeeHistory.ManagerSupervisorID
LEFT JOIN EMP_OWNER.EMP_OpenTeam ON EMP_OpenTeam.OpenTeamID = EMP_EmployeeHistory.ManagerSupervisorID
LEFT JOIN EMP_OWNER.EMP_Division ON EMP_Division.DivisionID = EMP_EmployeeHistory.DivisionID
LEFT JOIN EMP_OWNER.EMP_Department ON EMP_Department.DepartmentID = EMP_EmployeeHistory.DepartmentID
LEFT JOIN EMP_OWNER.EMP_Group ON EMP_Group.GroupID = EMP_EmployeeHistory.GroupID
LEFT JOIN EMP_OWNER.EMP_Function ON EMP_Function.FunctionID = EMP_EmployeeHistory.FunctionID
LEFT JOIN EMP_OWNER.EMP_Location ON EMP_Location.LocationID = EMP_EmployeeHistory.LocationID
LEFT JOIN EMP_OWNER.EMP_SubLocation ON EMP_SubLocation.SubLocationID = EMP_EmployeeHistory.SubLocationID
LEFT JOIN EMP_OWNER.EMP_Reason ON EMP_Reason.ReasonID = EMP_EmployeeHistory.ReasonID
WHERE EMP_EmployeeHistory.EmployeeID = 776
ORDER BY EMP_EmployeeHistory.EmployeeHistoryID DESC

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
:) yep, use "case when ... then ... when ... then ... end"
but too late.

see:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/expressions004.htm

note, should you need the word 'IF' to explain the requirement, good chance it will be solved by a case expression

NO points please
0
 

Author Closing Comment

by:dyarosh
Comment Utility
Thank you.  You saved me hours trying to figure it out!
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.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to take different types of Oracle backups using RMAN.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now