简体   繁体   中英

How to use two sub queries to pull values from two tables? T-SQL

First off I want to apologize if I'm asking the completely wrong question - I'm a beginner when it comes to SQL and I'm not sure how to accomplish my goal, but I am assuming based off my research that sub queries are what I need to be working with.

I have two tables, one with time card data (Table 1), and another with high level project data(Table 2).

Table 1 :

+------+------------------+---------------+-------------+
| code | work_description | resource_name | total_hours |
+------+------------------+---------------+-------------+
|  101 | Time Reporting   | Jane Doe      |           5 |
|  101 | Time Reporting   | Jane Doe      |           7 |
|  101 | Time Reporting   | Jane Doe      |           9 |
|  201 | Time Reporting   | Joe Smith     |           2 |
|  201 | Time Reporting   | Joe Smith     |           4 |
|  201 | Time Reporting   | Joe Smith     |           6 |
+------+------------------+---------------+-------------+

Table 2 :

+------+------------+----------------+
| code | project_id |     descr      |
+------+------------+----------------+
|  100 |        100 | Project A      |
|  101 |        100 | Time Reporting |
|  102 |        100 | Milestones     |
|  103 |        100 | Planning       |
|  200 |        200 | Project B      |
|  201 |        200 | Time Reporting |
|  202 |        200 | Milestones     |
|  203 |        200 | Planning       |
+------+------------+----------------+

In table 2, when the code column is equal to the project_id column, descr shows the project name. I need to pull all of table 1, in addition to the project name that corresponds to each row.

What I need:

+-----------+------+------------------+---------------+-------------+
|  descr    | code | work_description | resource_name | total_hours |
+-----------+------+------------------+---------------+-------------+
| Project A |  101 | Time Reporting   | Jane Doe      |           5 |
| Project A |  101 | Time Reporting   | Jane Doe      |           7 |
| Project A |  101 | Time Reporting   | Jane Doe      |           9 |
| Project B |  201 | Time Reporting   | Joe Smith     |           2 |
| Project B |  201 | Time Reporting   | Joe Smith     |           4 |
| Project B |  201 | Time Reporting   | Joe Smith     |           6 |
+-----------+------+------------------+---------------+-------------+

My though process is that first I would have to find the project_id that relates to each row in table 1. Then, I could use that value to match against project_id in table 2, so I could pull the project name out of the descr column

Here is what I have so far. This correctly pulls the project ID (I don't know if this is best practice). I have tried a few different sub queries for the project name, but I haven't been able to do it right yet.

SELECT  
    (SELECT t2.code WHERE t1.code=t2.code) as found_project_id,
    t2.descr,
    t1.code,
    t1.work_description,
    t1.resource_name,
    t1.total_hours
FROM Table1 as t1   
    INNER JOIN Table2 as t2 ON t1.code=t2.code

So my question is, how can I use sub queries (or any other method) to pull all of table 1 in addition to the project names?

You don't need a subquery here. A simple join is enough since an inner join already accomplishes what you are trying to do with the subquery:

SELECT
    proj.descr,
    t2.code,
    t1.work_description,
    t1.resource_name,
    t1.total_hours
FROM table2 t2

JOIN table1 t1 ON
    t1.code = t2.code
JOIN table2 proj ON
    proj.code = t2.project_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM