简体   繁体   中英

SQL SERVER- Lookup a derived column in another table and return a value

I have a query which has a column (PROCESS) which was obtained using a concat function. Now i need to lookup this column in a column which is on another table (Table2) and then return a value from the same table (table 2).

Query: 在此处输入图片说明

My Current output will look like this. 在此处输入图片说明

I have a reference Table like this.

在此处输入图片说明

I need to lookup "Process" (Query Result) in "Type" (the reference Table) and return "Description" (Ref table) in "Process Column".

Final Output should look like this 在此处输入图片说明

I'm unable to figure out how to modify my query to do this. pls help.

You need a join . Join your reference table to your other query based on the concatenated value and you can display the description you are looking for.

FROM #Source As s) D INNER JOIN [Reference Table] AS rt ON d.Process = rt.type

You need an INNER JOIN between Source and Reference tables for your nested query with the JOIN condition as in the following :

SELECT ... -- all columns of your queries outer part
(
SELECT s.Date, s.Station, s.worktype, s.tasktype, description as process, ....
  FROM source s
  INNER JOIN reference r on ( concat(s.worktype,s.tasktype) = r.type ) 
 ) D
 GROUP BY D.date, D.station, D.worktype, D.accountno;

SQL Fiddle Demo

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