Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Adding columns to a query from the results of another query

$
0
0
Alright, I'll try to explain this as simple a possible.

I have a table called approval_levels in which I have 2 columns: 

    
    |LEVEL_ID  |NAME          |
    |1         |app_level_1   |
    |2         |app_level_2   |
    |3         |app_level_3   |
   

I have a second table called requests, in which I have 3 columns: 

    |REQUEST_ID    |PRODUCT_NAME    |MANUFACTURER   |
    |1             |wd-40           |Acme           |
    |2             |Windex          |Acme           |
    |3             |Propane         |Acme           |
    

I have a third table which links the two called request_approvals which has 2 columns: REQUEST_ID, LEVEL_ID. When a request is approved for a specific level, I insert a value into this table. So, for example, lets say request 1 has been approved for all 3 levels, request 2 has been approved for only level 1, and request 3 has been approved for level 3, the table would show something like this.

    |REQUEST_ID    |LEVEL_ID       |
    |1             |1              |
    |1             |2              |
    |1             |3              |
    |2             |1              |
    |3             |3              |

Ok, so here the challenge: I need to show in a report all the requests, create a columns for each level and show whether or not that request is approved for that level. The end result has to be something like this:


    |REQUEST_ID |PRODUCT_NAME |MANUFACTURER |app_level_1 |app_level_2 |app_level_3 |
    |1          |wd-40        |Acme         |X           |X           |X           | 
    |2          |Windex       |Acme         |X           |            |            |
    |3          |Propane      |Acme         |            |            |X           | 

Keep in mind that if another value is added to the approval_levels table (ie app_level_4), I need to add another column to the table call app_level_4 dynamically.

Now, how in the world do I do something like this??? I don't even know where to start looking?

Thanks for the help!
       

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>