By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,246 Members | 1,806 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,246 IT Pros & Developers. It's quick & easy.

JOIN from 2 Tables and combine several records into 1 record

P: 1
Thank you for your assistance. I am a novice looking to JOIN and append or combine records from 2 current Tables into 2 new Tables named below. I have been able to JOIN Tables with the script below. My problem is if I don’t use the WHERE clause in my script below, the script can query up to 3 records with the same “LoanNo” due to the fact that there can be up to 3 “TypeCodes” in each Table (Borrower, Co-Borrower 1 and Co-Borrower 2 represented by 1, 2 and 3 in the “TypeCode” columns) or up to 6 records in each Table with the same “LoanNo” with the same aforementioned “TypeCode’s” however this happens because of a different date in the DX.FundDate column. The way it is now if there are 3 borrowers per loan, there can be 2 loans a 1st Lien and a 2nd Lien and I have to combine all of these records manually in Excel.

My goal is to append or combine these records to have all 3 “TypeCode’s” on 1 record with 19 columns in a new Table as follows:

Part 1- (DX.LoanNo, DX.FundDate, BR.BorrLastName, BR.BorrFirstName, BR.BorrMiddleName, BR.BorrSSN, BR.BorrTypeCode, DX.Address, DX.LienCode) = (TypeCode 1)

Part 2 – (BR.BorrLastName, BR.BorrFirstName, BR.BorrMiddleName, BR.BorrSSN, BR.BorrTypeCode) = (TypeCode 2)

Part 3 – (BR.BorrLastName, BR.BorrFirstName, BR.BorrMiddleName, BR.BorrSSN, BR.BorrTypeCode) = (TypeCode 3)

*NOTE: There is a “LienCode” in each Table (1st lien =1 and 2nd lien = 2). I would then have 2 new Tables representing Table.Lien 1 and Table.Lien 2. I eventually want to send these new Tables into Access via Excel as to not lose any data due to the Varchar 255 format in SQL. This is the way the DB came.

Current Tables (all are Varchar 255 nulls allowed):
Table 1=DX
DX.LoanNo - (no leading zeros or alphas)
DX.FullName - (text only with commas separating last, first and middle names)
DX.SSN - (some records have dashes and some don’t)
DX.TypeCode - (single digit from 1-3)
DX.FundDate - (date)
DX.Address - (alphanumeric)
DX.LienCode - (single digit from 1-2)

Table 2=BR (all are Varchar 255 nulls allowed)
BR.BorrLoanNo - (no leading zeros or alphas)- (same info as DX.LoanNo)
BR.BorrLastName - (text)
BR.BorrFirstName - (text)
BR.BorrMiddleName - (text)
BR.BorrSSN - (same as above) – (some fields are missing data or vice-versa with DX.SSN)
BR.BorrTypeCode - (single digit from 1-3) – (Same info as DX.TypeCode)

Current Script:
Expand|Select|Wrap|Line Numbers
  1. SELECT  DX.LoanNo, DX.FundDate, BR.BorrLastName, BR.BorrFirstName,
  2. BR.BorrMiddleName, BR.BorrSSN, BR.BorrTypeCode, DX.Address, DX.LienCode
  4. FROM         dbo.DX LEFT OUTER JOIN
  5.                       dbo.BR ON dbo.DX.LoanNo = dbo.BR.BorrLoanNo
  7. WHERE     (dbo.BR.BorrTypeCode = '1') AND (dbo.DX.Lien = '1')
  9. ORDER BY dbo.DX.LoanNo
I have MS SQL Server Management Studio and MS Office 2007 on Windows XP Pro. Again thank you for your help.

Mar 18 '08 #1
Share this Question
Share on Google+
1 Reply

Expert 2.5K+
P: 2,878
Sometimes it's easier to just post some sample data, even if those are just mock values and what you're trying to achieve. Sorry, it would be faster for me to read that way rather than goes through this. Maybe the others can help.

-- CK
Mar 19 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.