Solved

Very complex cross database select statement

Posted on 2010-11-30
2
211 Views
Last Modified: 2012-05-10
In my select statement below I need the ability to change the database I'm selecting from on a record by record basis.

So...when Left(p.afsSource,2) = 'LT'
I pull from proc_lt.dbo.P_PaymentStreams

Else
I pull from  crmprod_01.dbo.P_PaymentStreams


The top part will ALWAYS pull from proc_cfa.dbo.P_AvailableForSale
Declare @OrdTranID int  

Set @OrdTranID = 9001

Select  Left(p.afsSource,2) as busType,    

        SubString(p.afsSource,4,4) as DealID,  

		Sum(p.afsAMount) as afsAmount,

		st.cp as CompoundingMethod         

from    proc_cfa.dbo.P_AvailableForSale p   

        LEFT JOIN (  

                Select              

                        ps.DealID    

                        ,CASE WHEN COUNT(ps.PmtFrequency) > 1 THEN 'Split' ELSE MIN(ps.PmtFrequency) END cp    

                        ,MAX(ps.loanDate) LoanDate  

                from    proc_lt.dbo.P_PaymentStreams ps  

                Group by ps.DealID   

        ) st ON st.DealId = SubString(p.afsSource,4,4)  

where   p.afsTransAction = @OrdTranID    

group by Left(p.afsSource,2), SubString(p.afsSource,4,4), st.cp

Open in new window

0
Comment
Question by:lrbrister
2 Comments
 
LVL 7

Accepted Solution

by:
Cboudroz earned 500 total points
Comment Utility
there you go
Declare @OrdTranID int  
Set @OrdTranID = 9001
Select  Left(p.afsSource,2) as busType,    
        SubString(p.afsSource,4,4) as DealID,  
		Sum(p.afsAMount) as afsAmount,
		ISNULL(st.cp,st2.cp) as CompoundingMethod         
from    proc_cfa.dbo.P_AvailableForSale p   

        LEFT JOIN (  
                Select              
                        ps.DealID    
                        ,CASE WHEN COUNT(ps.PmtFrequency) > 1 THEN 'Split' ELSE MIN(ps.PmtFrequency) END cp    
                        ,MAX(ps.loanDate) LoanDate  
                from    proc_lt.dbo.P_PaymentStreams ps  
                Group by ps.DealID   
        ) st ON st.DealId = SubString(p.afsSource,4,4) 
				AND Left(p.afsSource,2) = 'LT'

        LEFT JOIN (  
                Select              
                        ps2.DealID    
                        ,CASE WHEN COUNT(ps2.PmtFrequency) > 1 THEN 'Split' ELSE MIN(ps2.PmtFrequency) END cp    
                        ,MAX(ps2.loanDate) LoanDate  
                from    crmprod_01.dbo.P_PaymentStreams ps2
                Group by ps2.DealID   
        ) st2 ON st2.DealId = SubString(p.afsSource,4,4) 
				AND Left(p.afsSource,2) <> 'LT'
 
where   p.afsTransAction = @OrdTranID    
group by Left(p.afsSource,2), SubString(p.afsSource,4,4), isnull(st.cp, st2.cp)

Open in new window

0
 

Author Closing Comment

by:lrbrister
Comment Utility
Great!
Anything you ever need...mayhem, assault, robbery...just let me know
:-D
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 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

18 Experts available now in Live!

Get 1:1 Help Now