Q:

Select from two tables based on values of one of the table

1) Table1 say table1 with structure as :
moduleID | moduleName 
   10    |      XYZ
   20    |      PQR
   30    |      ABC 
2) Table2 say table2 with structure as :
moduleID | Level | Value 
   10    |   1    |  20
   10    |   2    |  30
   30    |   3    |  40
   10    |   3    |  50
   20    |   2    |  30

moduleID being primary key in table1,and value of the column level can have values 1 to 3.
Now it is required to display the data as follows :  
moduleID | moduleName | Level1  | Level2  | Level3 
   10    |      XYZ    | 20      |  30   |   50
   20    |      PQR    | NULL    |  30   |   NULL
   30    |      ABC    | NULL    |  NULL |   50

In simpler terms, values of column Level in table2 is displayed as Level1, Level2 and Level3 and values corresponding to each level is populated in the corresponding moduleID row.
Any help on this? beginner here in SQL. Something to do with Views?

A:

You can use conditional aggregation:
select t1.moduleID, t1.moduleName,
       MAX(CASE WHEN Level = 1 THEN Value END) Level1,
       MAX(CASE WHEN Level = 2 THEN Value END) Level2,
       MAX(CASE WHEN Level = 3 THEN Value END) Level3
from table1 as t1
left join table2 as t2 on t1.moduleID = t2.moduleID
group by t1.moduleID, t1.moduleName

