Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Complex Left Join

Posted on 2010-11-30
10
Medium Priority
?
462 Views
Last Modified: 2012-05-10
I need to do a left join between my two attached select statements on

First Select                             Second Select
SubString(afsSource,4,4)  =  DealID
Select  Left(afsSource,2) as busType,  
        SubString(afsSource,4,4) as DealID,
		Sum(afsAMount) as afsAmount       
from    proc_cfa.dbo.P_AvailableForSale  
where   afsTransAction = @OrdTranID  
group by Left(afsSource,2), SubString(afsSource,4,4)


Select            
        DealID  
        ,CASE WHEN COUNT(PmtFrequency) > 1 THEN 'Split' ELSE MIN(PmtFrequency) END cp  
        ,MAX(loanDate) LoanDate
from    proc_lt.dbo.P_PaymentStreams 
Group by DealID 
order by DealID

Open in new window

0
Comment
Question by:lrbrister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 4

Expert Comment

by:patterned
ID: 34239640
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 1400 total points
ID: 34239679
You could use a CTE (connon table expression) to make it easier:
;WITH cte_PaymentStreams (DealID,cp,LoanDate) AS
(
	Select            
			DealID  
			,CASE WHEN COUNT(PmtFrequency) > 1 THEN 'Split' ELSE MIN(PmtFrequency) END cp  
			,MAX(loanDate) LoanDate
	from    proc_lt.dbo.P_PaymentStreams 
	Group by DealID 
	order by DealID
)
Select  Left(afsSource,2) as busType,  
        SubString(afsSource,4,4) as DealID,
		Sum(afsAMount) as afsAmount       
from    proc_cfa.dbo.P_AvailableForSale A
LEFT JOIN cte_PaymentStreams B ON SubString(A.afsSource,4,4)  =  B.DealID 
where   afsTransAction = @OrdTranID  
group by Left(afsSource,2), SubString(afsSource,4,4)

Open in new window

0
 
LVL 32

Expert Comment

by:Erick37
ID: 34239691
Correction: CTE (common table expression)
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 18

Expert Comment

by:deighton
ID: 34239733
don't do a left join, join on all rows, then select time matches

select ...From A,B Where YourTimeDifference(A.Time, B.Time) < 10

then you could use the UNION statement to add rows from first table that are not matched

or you could make the first select into a table variable, then select all rows from it Unioned with rows not counted in it




0
 

Author Comment

by:lrbrister
ID: 34239752
Erick37:
How do I get the cp column from the top select?
0
 
LVL 13

Assisted Solution

by:gamarrojgq
gamarrojgq earned 600 total points
ID: 34239772
Try this, with this approach you can add the columns from the inner query to the main query but with an aggregate function since you main query have a Group clause
Select  Left(P_AvailableForSale.afsSource,2) as busType,  
        SubString(P_AvailableForSale.afsSource,4,4) as DealID,
		Sum(P_AvailableForSale.afsAMount) as afsAmount       
from    proc_cfa.dbo.P_AvailableForSale  
	LEFT JOIN (
		Select            
		        P_PaymentStreams.DealID  
		        ,CASE WHEN COUNT(P_PaymentStreams.PmtFrequency) > 1 THEN 'Split' ELSE MIN(P_PaymentStreams.PmtFrequency) END cp  
		        ,MAX(P_PaymentStreams.loanDate) LoanDate
		from    proc_lt.dbo.P_PaymentStreams 
		Group by P_PaymentStreams.DealID 
	) SecondTable ON SecondTable.DealId = SubString(P_AvailableForSale.afsSource,4,4)
where   P_AvailableForSale.afsTransAction = @OrdTranID  
group by Left(P_AvailableForSale.afsSource,2), SubString(P_AvailableForSale.afsSource,4,4)

Open in new window

0
 
LVL 32

Expert Comment

by:Erick37
ID: 34239831
Like this:
;WITH cte_PaymentStreams (DealID,cp,LoanDate) AS
(
	Select            
			DealID  
			,CASE WHEN COUNT(PmtFrequency) > 1 THEN 'Split' ELSE MIN(PmtFrequency) END cp  
			,MAX(loanDate) LoanDate
	from    proc_lt.dbo.P_PaymentStreams 
	Group by DealID 
	order by DealID
)
Select  Left(afsSource,2) as busType,  
        SubString(afsSource,4,4) as DealID,
	Sum(afsAMount) as afsAmount,   
        B.cp,
        B.LoanDate,
        B.DealID    
from    proc_cfa.dbo.P_AvailableForSale A
LEFT JOIN cte_PaymentStreams B ON SubString(A.afsSource,4,4)  =  B.DealID 
where   afsTransAction = @OrdTranID  
group by Left(afsSource,2), SubString(afsSource,4,4)

Open in new window

0
 

Author Comment

by:lrbrister
ID: 34239838
Erick37: and gamarrojgq:

Both answers worked fine...and valid applications.

I would like to award points to Erick37: with a nod to gamarrojgq with some points as well.

Anu problems with a 350/150 split?

I am also going to post an additional question based on this solution
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 34239877
Irbrister,

It sounds fine to me :)
0
 

Author Closing Comment

by:lrbrister
ID: 34240038
Excellent guys.  Thanks
0

Featured Post

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

662 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