query question, possible subquery?

  • I have 2 tables that are laid out as:

    JobCode JobDescription JobFamilyCode

    1a developer 2m

    3b accounting 2a

    1b marketing 1m

    and table 2 looks like

    JobParent Description

    1a_2m developer, builds and maintains, .......

    3b_2a provide CFO all financial .........

    1b_1M create company brand for online and.....

    I'm able to combine JobCode and JobFamilyCode so it looks like [1a_2m], is there a way to get that result and then

    do a select on table 2 within the same query. I'd like to keep the call if possible in one query.

  • You really should leave the columns separate in the other table as well, but here's how to do the join with the combined column:

    SELECT ...

    FROM table1 t1

    INNER JOIN table2 t2 ON

    t2.JobParent = t1.JobCode + '_' + t1.JobFamilyCode

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • ScottPletcher (3/2/2015)


    You really should leave the columns separate in the other table as well, but here's how to do the join with the combined column:

    SELECT ...

    FROM table1 t1

    INNER JOIN table2 t2 ON

    t2.JobParent = t1.JobCode + '_' + t1.JobFamilyCode

    thanks, when I tried that I'm getting an error on the { incorrect syntax near '_' }.

  • SQL_NuB (3/2/2015)


    ScottPletcher (3/2/2015)


    You really should leave the columns separate in the other table as well, but here's how to do the join with the combined column:

    SELECT ...

    FROM table1 t1

    INNER JOIN table2 t2 ON

    t2.JobParent = t1.JobCode + '_' + t1.JobFamilyCode

    thanks, when I tried that I'm getting an error on the { incorrect syntax near '_' }.

    What RDBMS are you using?

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (3/2/2015)


    SQL_NuB (3/2/2015)


    ScottPletcher (3/2/2015)


    You really should leave the columns separate in the other table as well, but here's how to do the join with the combined column:

    SELECT ...

    FROM table1 t1

    INNER JOIN table2 t2 ON

    t2.JobParent = t1.JobCode + '_' + t1.JobFamilyCode

    thanks, when I tried that I'm getting an error on the { incorrect syntax near '_' }.

    What RDBMS are you using?

    SQL 08

  • I assume you replaced the '...' with your own columns :-).

    What specific error are you getting?

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • I get:

    Query:

    SELECT Jobs.JobFamily, JobDescriptions.[description]

    FROM Jobs t1

    INNER JOIN JobDescriptions t2 ON

    t2.ParentRole = t1.JobFamily + '_' + t1.[description]

    Error

    Msg 4104, Level 16, State 1, Line 1

    The multi-part identifier "Jobs.JobFamily" could not be bound.

    Msg 4104, Level 16, State 1, Line 1

    The multi-part identifier "JobDescriptions.description" could not be bound.

  • You've added aliases Jobs->t1 and JobDescriptions->t2 so in SELECT SQL see tables Jobs, JobDescriptions as t1, t2.

    Try:

    SELECT t1.JobFamily, t2.[description]

    FROM Jobs t1

    INNER JOIN JobDescriptions t2 ON

    t2.ParentRole = t1.JobFamily + '_' + t1.[description]

Viewing 8 posts - 1 through 7 (of 7 total)

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