Results 1 to 4 of 4

Thread: Merge Tables

  1. #1
    Join Date
    Mar 2008
    Posts
    40

    Unanswered: Merge Tables

    Code:
    Grade_table
    ******************
    Student_ID | Grade
    ------------------
    t106       | 100
    t107       | 85
    t108       | 75
    t109       | 60
    Code:
    Student_archive
    *******************
    Student_ID | Gender
    -------------------
    t106       | Male
    t109       | Female
    Code:
    Student
    Student_ID | Gender
    *******************
    t108       | Female
    t107       | Male
    Now I need to Join 3 tables which would look like following

    Code:
    Student_ID | Gender | Grade
    ---------------------------
    t106       | Male   | 100
    t107       | Male   | 85
    t108       | Female | 75
    t109       | Female | 60
    I believe I should check for ISNULL and if it's NULL select gender From other tables. But I think there are smarter people in this forum than me who would have better Idea.
    Any help?
    Last edited by gunbilegt; 04-29-11 at 08:08.

  2. #2
    Join Date
    Mar 2008
    Posts
    40
    Code:
    SELECT * FROM Grade_table
    LEFT OUTER JOIN
    (SELECT Student_ID, Gender FROM Student
    UNION ALL
    SELECT Student_ID, Gender FROM Student_archive) As Students
    ON Grade_table.Student_id = Students.Student_ID


    Got it.

  3. #3
    Join Date
    Nov 2004
    Posts
    1,427
    Provided Answers: 4
    With this data:
    Code:
    Student_archive
    *******************
    Student_ID | Gender
    -------------------
    t106       | Male
    t109       | Female
    
    
    Student
    Student_ID | Gender
    *******************
    t106       | Male
    t107       | Male
    t108       | Female
    t109       | Female
    The SQL query will produce duplicate rows. (Hint: remove the ALL after UNION, unless you can be 100% sure there will never be an overlap between those two tables.)
    With kind regards . . . . . SQL Server 2000/2005/2012
    Wim

    Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
    Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

  4. #4
    Join Date
    Mar 2008
    Posts
    40
    That's a good point. I shouldn't have named this topic merge tables. So I apologize for that.

    Actually I needed View not table. Just a select not an insert or merge.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •