Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Lookup for multiple columns from a reference table and create new columns- SQL

$
0
0

I need help to modify my query to lookup multiple columns from a reference table and create new columns

Result of my Query
Date Station Worktype tasktype Process Accountno Company Contractor
2-Jul ARRT PI 12 Prcoess Improvement 12345 1 0

I have a reference Table like this.

Reference Table

Worktype Worktypedesc tasktype tasktypedesc process Desc

PI Process Imp 12 Repair PI12 process Improvement

I need to lookup Worktype (Query Result) in "Worktype" (ref table) , then return"Worktypedesc" (ref table) as a new column in my output . Similarly do for tasktype.

I need in modifying my query to produce this result

Final Result after vlookup
Date Station Worktype Worktypedesc  tasktype tasktypedesc Process desc   Accountno Company Contractor
2-Ju ARRT PI            process imp 12          repair PI12    PI 12345 1 0

query:

SELECT 
D.Date, D.Station, D.worktype ,D.tasktype, D.Accountno, t.description as process , SUM(D.contractorCount) AS contractor , SUM(D.companyCount) AS company (
SELECT 
S.Date, S.Station, S.worktype , s.tasktype , concat(worktype,tasktype)as process , S.Accountno, CASE S.workdoneby 
WHEN 'Contractor' THEN 1
ELSE 0END AS contractorCount , CASE S.workdoneby 
WHEN 'Company' THEN 1
ELSE 0END AS companyCount 
FROM #Source AS S) D 
INNER JOIN ReferenceTable t
ON t.[Type]= D.[process]
GROUP BY 
D.Date, D.Station, D.worktype ,D.tasktype, D.Accountno, t.description


Viewing all articles
Browse latest Browse all 7129

Trending Articles