Get data from vertical to horizontal table

  • Hello Friends,

    I have a table A as follows:

    ID Name

    1 A

    2 B

    3 C

    4 D

    I have another table B as follows:

    ID Message Date JobStatus

    1 Good 01/01/2009 Completed

    2 Good 02/03/2009 Pending

    3 Poor 01/03/2009 Approved

    4 Good 02/02/2009 Completed

    From the above two tables, I want to create a new table as follows:

    Name Message Date JobStatus

    A Good 01/01/2009 Completed

    B Good 02/03/2009 Pending

    C Poor 01/03/2009 Approved

    D Good 02/02/2009 Completed

    Which means that I want to know if there is a way to write a SQL query that will help me to get the Names of the ID's by matching the ID's.

    To be more clear, in the new table that I will be creating, I want to see that if in table A, ID is 1, then it should check table B, ID and retrieve the name from it as A.

    So, in place of ID's 1, 2, 3 or 4, I want to see the names A, B, C or D.

    I would be here to answer any questions if I am unclear in my question above.

    Thanks,

    notes4we

  • use a join for this;

    select Name, Message, Date, JobStatus

    from tableA inner join tableB on tableA.id = tableb.id

  • steveb (2/25/2009)


    use a join for this;

    select Name, Message, Date, JobStatus

    from tableA inner join tableB on tableA.id = tableb.id

    There columns Message, Date, JobStatus are not in table A, all are in table B.

  • One of the main reasons to use a join is to get data from more than one table. The above query should work assuming that the columns specified are not in both tables. You could fully qualify them to be sure like this:

    SELECT TableA.Name, TableB.Message, TableB.Date, TableB.JobStatus

    FROM TableA INNER JOIN

    TableB ON TableA.ID = TableB.ID

    Greg

  • notes4we (2/25/2009)


    steveb (2/25/2009)


    use a join for this;

    select Name, Message, Date, JobStatus

    from tableA inner join tableB on tableA.id = tableb.id

    There columns Message, Date, JobStatus are not in table A, all are in table B.

    That is the point of the Join it allows you to select columns from all the tables in the join

  • Thank you for all your replies.

    I know this was a silly question, but I am still learning, so........

    Thanks once again.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply