JOIN from 2 Tables and combine several records into 1 record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bluereign
    New Member
    • Mar 2008
    • 1

    JOIN from 2 Tables and combine several records into 1 record

    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.BorrFirstNam e, BR.BorrMiddleNa me, BR.BorrSSN, BR.BorrTypeCode , DX.Address, DX.LienCode) = (TypeCode 1)

    Part 2 – (BR.BorrLastNam e, BR.BorrFirstNam e, BR.BorrMiddleNa me, BR.BorrSSN, BR.BorrTypeCode ) = (TypeCode 2)

    Part 3 – (BR.BorrLastNam e, BR.BorrFirstNam e, BR.BorrMiddleNa me, 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.BorrFirstNam e - (text)
    BR.BorrMiddleNa me - (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:
    Code:
    SELECT  DX.LoanNo, DX.FundDate, BR.BorrLastName, BR.BorrFirstName,
    BR.BorrMiddleName, BR.BorrSSN, BR.BorrTypeCode, DX.Address, DX.LienCode
    
    FROM         dbo.DX LEFT OUTER JOIN
                          dbo.BR ON dbo.DX.LoanNo = dbo.BR.BorrLoanNo
    
    WHERE     (dbo.BR.BorrTypeCode = '1') AND (dbo.DX.Lien = '1')
    
    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.

    Blue
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    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

    Comment

    Working...