Solved

Very complex cross database select statement

Posted on 2010-11-30
2
213 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
ID: 34240128
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
ID: 34240229
Great!
Anything you ever need...mayhem, assault, robbery...just let me know
:-D
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Deadlocks 12 49
Updating a column using a lookup on another table. 6 17
TSQL - IF ELSE? 3 27
execute SQL Stored Procedure 1000 times from vb.net 3 19
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

911 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

20 Experts available now in Live!

Get 1:1 Help Now