Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Need help with multi database query.

$
0
0

Hello,

I am working on a query between multiple databases to be view on a web app.  We have multiple web apps and we are integrating some functionality between them all into one page. 

The data will be for a delegate someone sets for themselves (they can have more then one also).  I need a way to pull the data view it as follows.

User, UserNumber, DelegateName, ForApp, ForApp, ForApp, ForApp, ForApp, ForApp

I have had no problem pulling the delegates themselves and which apps they are a delegate for,  but not which person they are the delegate for.

SELECT DISTINCT 
                      dbo.Employee.Position_Num AS Delegate_Position, dbo.Employee.Name AS [Delegate Name], 
                      (CASE WHEN Hierarchy.dbo.Employee.Position_Num = CVI_Delegate.Delegate THEN 1 ELSE 0 END) AS For_CVI, 
                      (CASE WHEN Hierarchy.dbo.Employee.Position_Num = Travel_Delegate.Delegate THEN 1 ELSE 0 END) AS For_Travel, 
                      (CASE WHEN Hierarchy.dbo.Employee.Position_Num = Dept_Deposits_Delegate.Delegate THEN 1 ELSE 0 END) AS For_DeptDeposit, 
                      dbo.OKCorral_Delegate.For_UserRoles AS For_OkCorralUR, dbo.OKCorral_Delegate.For_FiscalApprover AS For_OkCorralFA, 
                      (CASE WHEN Hierarchy.dbo.Employee.Position_Num = Reqs_Delegate.Delegate THEN 1 ELSE 0 END) AS For_Reqs
FROM         dbo.Employee LEFT OUTER JOIN
                      Reqs.dbo.Delegate AS Reqs_Delegate ON dbo.Employee.Position_Num = Reqs_Delegate.Position LEFT OUTER JOIN
                      DeptDeposits.dbo.Delegate AS Dept_Deposits_Delegate ON dbo.Employee.Position_Num = Dept_Deposits_Delegate.Position LEFT OUTER JOIN
                      CVI.dbo.Delegate AS CVI_Delegate ON dbo.Employee.Position_Num = CVI_Delegate.Position LEFT OUTER JOIN
                      dbo.OKCorral_Delegate ON dbo.Employee.Position_Num = dbo.OKCorral_Delegate.Position LEFT OUTER JOIN
                      Travel.dbo.Delegate AS Travel_Delegate ON dbo.Employee.Position_Num = Travel_Delegate.Position

This query works fine.  The problem I run into is I have to use the same Employee table to get the EmployeeName and Number

Which will be used for both the person and the delegate.  Any ideas will be a great help.  It had been suggested I use a procedure to accomplish this, but I have no idea where to start that at.  I also have tried a nested sbu-query but since a person can have more then one delegate for an app,  this through errors.  Thanks.


George Fields


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>