I have 6 tables (t1, t2, t3, …,t6 in the same SQL Server 2008 R2 database. I wanted to group them into two. Group 1 (TG1) includes tables 1, 2 and 3. Group 1 (TG1) includes tables 4, 5 and six.
I have created two queries each with union
SELECT TG1.*
FROM (SELECT t1.PID AS ID, t1.FVALUE AS FVALUE, t1.TVALUE AS TVALUE
FROM mydb.myuser.t1
UNION
SELECT t2.PID AS ID, t2.FVALUE AS FVALUE, t2.TVALUE AS TVALUE
FROM mydb.myuser.t2
UNION
SELECT t3.PID AS ID, t3.FVALUE AS FVALUE, t3.TVALUE AS TVALUE
FROM mydb.myuser.t3) AS TG1
SELECT TG2.*
FROM (SELECT t4.PID AS PID, t4.VALUE AS VALUE, t4.VALUEID AS VALUEID, t4.VALUEDESC AS VALUEDESC
FROM mydb.myuser.T4
SELECT t4.PID AS PID, t4.VALUE AS VALUE, t4.VALUEID AS VALUEID, t4.VALUEDESC AS VALUEDESC
FROM mydb.myuser.T4
SELECT t4.PID AS PID, t4.VALUE AS VALUE, t4.VALUEID AS VALUEID, t4.VALUEDESC AS VALUEDESC
FROM mydb.myuser.T4) AS TG2
I am getting the following two tables
The final query that I want to create is as shown in the screenshot below by taking all the values of TG1 and populate additional fields from TG2 that match the values of TG1.
The values in above screenshot were populated in Excel using vlookup and I need help to do the same in SQL query. I appreciate your help.
Best Answer
Check: